Skip to content

Instantly share code, notes, and snippets.

Forked from sarahholderness/
Created March 23, 2017 10:31
What would you like to do?
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 = {}
email_file = open('emails.txt', 'r')
for line in email_file:
(email, name) = line.split(',')
emails[email] = name.strip()
except FileNotFoundError as err:
return emails
def get_schedule():
# Reading our schedule from a file
schedule_file = open('schedule.txt', 'r')
schedule =
except FileNotFoundError as 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(), Flying Python, Sarah Buchanan, 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('', '587')
# Start TLS encryption
# Login
password = input("What's your password?")
from_email = ''
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)
import requests
def get_weather_forecast():
# Connecting to the weather api
url = ',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