Skip to content

Instantly share code, notes, and snippets.

Last active December 4, 2022 10:49
Show Gist options
  • Save sarahholderness/21f78b1d06ff2985e4c9 to your computer and use it in GitHub Desktop.
Save sarahholderness/21f78b1d06ff2985e4c9 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 = {}
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
Copy link

Hi Sarah !
First thanks for sharing your code,

I had an issue when running your code. Can you help on this ? :)

C:\Python27\python.exe D:/Gist/emailer/
What's your password?test
Traceback (most recent call last):
  File "D:/Gist/emailer/", line 52, in <module>
  File "D:/Gist/emailer/", line 49, in main
    smtp.send_emails(emails, schedule, forecast)
  File "D:\Gist\emailer\", line 11, in send_emails
    password = input("What's your password?")
  File "<string>", line 1, in <module>
NameError: name 'test' is not defined

Copy link

@casdidier, I think you had typo in Please check

Copy link

bhaumin commented Apr 23, 2016

@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.

Copy link

bbelderbos commented Mar 23, 2017

@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 key that is exposed in source)

Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment