-
-
Save sarahholderness/21f78b1d06ff2985e4c9 to your computer and use it in GitHub Desktop.
| import weather | |
| import smtp | |
| ''' | |
| Send a greeting email to our customer email list | |
| with the daily weather forecast and schedule | |
| ''' | |
| def get_emails(): | |
| # Reading emails from a file | |
| emails = {} | |
| try: | |
| email_file = open('emails.txt', 'r') | |
| for line in email_file: | |
| (email, name) = line.split(',') | |
| emails[email] = name.strip() | |
| except FileNotFoundError as err: | |
| print(err) | |
| return emails | |
| def get_schedule(): | |
| # Reading our schedule from a file | |
| try: | |
| schedule_file = open('schedule.txt', 'r') | |
| schedule = schedule_file.read() | |
| except FileNotFoundError as err: | |
| print(err) | |
| return schedule | |
| def main(): | |
| # Get our dictionary of customer emails and names | |
| emails = get_emails() | |
| # Get our daily performance schedule | |
| schedule = get_schedule() | |
| # Get the current weather forecast | |
| forecast = weather.get_weather_forecast() | |
| # Send emails to all of our customers with our forecast and schedule | |
| smtp.send_emails(emails, schedule, forecast) | |
| main() |
| [email protected], Flying Python | |
| [email protected], Sarah Buchanan | |
| [email protected], hal |
| Ventriloquism - 9:00am | |
| Snake Charmer - 12:00pm | |
| Amazing Acrobatics - 2:00pm | |
| Enchanted Elephants - 5:00pm |
| import smtplib | |
| def send_emails(emails, schedule, forecast): | |
| # Connect to the smtp server | |
| server = smtplib.SMTP('smtp.gmail.com', '587') | |
| # Start TLS encryption | |
| server.starttls() | |
| # Login | |
| password = input("What's your password?") | |
| from_email = '[email protected]' | |
| server.login(from_email, password) | |
| # Send to entire email list | |
| for to_email, name in emails.items(): | |
| message = 'Subject: Welcome to the Circus!\n' | |
| message += 'Hi ' + name + '!\n\n' | |
| message += forecast + '\n\n' | |
| message += "Today's Performance Schedule:" | |
| message += schedule + '\n\n' | |
| message += 'Hope to see you there!' | |
| server.sendmail(from_email, to_email, message) | |
| server.quit() |
| import requests | |
| def get_weather_forecast(): | |
| # Connecting to the weather api | |
| url = 'http://api.openweathermap.org/data/2.5/weather?q=Orlando,fl&units=imperial&appid=f641b59e03463c808393605f493b1f93' | |
| weather_request = requests.get(url) | |
| weather_json = weather_request.json() | |
| # Parsing JSON | |
| description = weather_json['weather'][0]['description'] | |
| temp_min = weather_json['main']['temp_min'] | |
| temp_max = weather_json['main']['temp_max'] | |
| # Creating our forecast string | |
| forecast = 'The Circus forecast for today is ' | |
| forecast += description + ' with a high of ' + str(int(temp_max)) | |
| forecast += ' and a low of ' + str(int(temp_min)) + '.' | |
| return forecast |
@casdidier, I believe this code is for Python 3. So either run with Python 3+ interpreter.
Or, you can try changing input() to raw_input() for Python 2.7.
@casdidier to support both input() and raw_input() you can do:
from builtins import input
(edit: seems you need to install that for py2, so maybe do a try/except on raw_input rather)
However I would probably get the pw from my OS environment so I can run this automatically / in a cronjob:
password = os.environ.get('MAIL_PASSWORD')
(or make it a constant at the top of the script)
(edit2: same for api.openweathermap.org API key that is exposed in source)
Can this program be written calling only one txt file? i meant email.txt and schedule.txt merged in one file and the mails to be sent to only selected mail IDs?
@casdidier, I think you had typo in smtp.py. Please check