├── README.md ├── cron_test.py ├── lambda_function.py ├── send_dog_email.py ├── send_email.py └── windows_test.py /README.md: -------------------------------------------------------------------------------- 1 | # Automating Python Code 2 | 3 | Code snippets to go along with my video *How to Schedule & Automatically Run Python Code!* Link to video here: https://youtu.be/aqnJvXOIr6g 4 | 5 | Also some code was added to a branch in my *generate-analytics-report* repo. The link to that code is here: https://github.com/KeithGalli/generate-analytics-report/tree/send_email 6 | 7 | If you have any questions, leave a comment on the video! 8 | -------------------------------------------------------------------------------- /cron_test.py: -------------------------------------------------------------------------------- 1 | with open('/mnt/c/Users/keith/code/cron_test.txt', 'a') as f: 2 | f.write("Hello World!\n") -------------------------------------------------------------------------------- /lambda_function.py: -------------------------------------------------------------------------------- 1 | import os 2 | import smtplib 3 | import requests 4 | from email.message import EmailMessage 5 | 6 | def lambda_handler(event, context): 7 | dog_info = requests.get('https://api.thedogapi.com/v1/images/search').json()[0] 8 | dog_image_url = dog_info['url'] 9 | 10 | EMAIL_ADDRESS = "kgmit18@gmail.com" 11 | EMAIL_PASSWORD = os.environ.get('EMAIL_PASSWORD') 12 | 13 | msg = EmailMessage() 14 | msg['Subject'] = "Dog of the day! (lambda)" 15 | msg['From'] = EMAIL_ADDRESS 16 | msg['To'] = ['kgmit18@gmail.com'] 17 | 18 | msg.set_content(f'Here is your dog!\n{dog_image_url}') 19 | msg.add_alternative(f'Here is your dog!
', subtype='html') 20 | 21 | with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp: 22 | smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD) 23 | smtp.send_message(msg) 24 | -------------------------------------------------------------------------------- /send_dog_email.py: -------------------------------------------------------------------------------- 1 | import os 2 | import smtplib 3 | import requests 4 | from email.message import EmailMessage 5 | 6 | dog_info = requests.get('https://api.thedogapi.com/v1/images/search').json()[0] 7 | dog_image_url = dog_info['url'] 8 | 9 | EMAIL_ADDRESS = "kgmit18@gmail.com" 10 | EMAIL_PASSWORD = os.environ.get('EMAIL_PASSWORD') 11 | 12 | msg = EmailMessage() 13 | msg['Subject'] = "Dog of the day!" 14 | msg['From'] = EMAIL_ADDRESS 15 | msg['To'] = ['kgmit18@gmail.com'] 16 | 17 | msg.set_content(f'Here is your dog!\n{dog_image_url}') 18 | msg.add_alternative(f'Here is your dog!
', subtype='html') 19 | 20 | with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp: 21 | smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD) 22 | smtp.send_message(msg) 23 | -------------------------------------------------------------------------------- /send_email.py: -------------------------------------------------------------------------------- 1 | import os 2 | import smtplib 3 | from email.message import EmailMessage 4 | 5 | EMAIL_ADDRESS = "kgmit18@gmail.com" 6 | EMAIL_PASSWORD = os.environ.get('EMAIL_PASSWORD') 7 | 8 | msg = EmailMessage() 9 | msg['Subject'] = "Test email!" 10 | msg['From'] = EMAIL_ADDRESS 11 | msg['To'] = ['kgmit18@gmail.com'] 12 | 13 | msg.set_content('This is a test email!') 14 | 15 | with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp: 16 | smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD) 17 | smtp.send_message(msg) 18 | -------------------------------------------------------------------------------- /windows_test.py: -------------------------------------------------------------------------------- 1 | path = 'C:/Users/keith/code/windows_test.txt' 2 | 3 | with open(path, 'a') as f: 4 | f.write("yoyoyoyoyo yo!\n") --------------------------------------------------------------------------------