Skip to content

Instantly share code, notes, and snippets.

@adityagupta679
Forked from sarahholderness/emailer.py
Created November 19, 2016 11:35
Show Gist options
  • Save adityagupta679/edbce042cddbaa0c86a6a4930936e047 to your computer and use it in GitHub Desktop.
Save adityagupta679/edbce042cddbaa0c86a6a4930936e047 to your computer and use it in GitHub Desktop.
Python scripts to read a list of customer emails and send an email with the daily weather forecast
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()
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment