Skip to content

Instantly share code, notes, and snippets.

@samueljackson92
Created November 23, 2013 09:02
Show Gist options
  • Save samueljackson92/7612418 to your computer and use it in GitHub Desktop.
Save samueljackson92/7612418 to your computer and use it in GitHub Desktop.
Secret Santa program. Takes a list of names and emails and some details to connect to an SMTP server and pairs each person with another person and sends out emails to each participant informing them who they are secret Santa for.
{
"email": "example@gmail.com",
"password": "yourpasswordhere",
"server": "smtp.gmail.com",
"port": 587
}
John Doe, john.doe@example.com
Jane Doe, jane.doe@example.com
Joe Bloggs, joe.bloggs@example.com
# Secret Santa Program
# Author: Samuel Jackson
# Date: 2013-11-13
import random
import smtplib
import json
import sys
from email.mime.text import MIMEText
# rotate list l by n elements
def rotate(l, n):
return l[n:] + l[:n]
# load a list of names from file, seperated by a newline
def loadData(fname):
try:
with open(fname) as fhandle:
lines = fhandle.readlines()
lines = [line.strip().split(',') for line in lines]
lines = map(list, zip(*lines))
except EnvironmentError, e:
sys.exit("Oh no! Unexpected exception loading names file: " + str(e))
return lines
#load config data from json file
def loadJsonData(fname):
try:
with open(fname) as fhandle:
jsonData = json.load(fhandle)
except EnvironmentError, e:
sys.exit("Oh no! Unexpected exception loading config file: " + str(e))
return jsonData
# given a list of recipients and messages, send each one
def sendMail(emails):
configData = loadJsonData('config.json')
print "Conncting to STMP server..."
#start up & login to smtp server
server = smtplib.SMTP(configData["server"], configData["port"])
server.starttls()
server.login(configData["email"], configData["password"])
print "Connected."
#send emails to each of the recipients
for reciever, message in emails:
msg = MIMEText(message)
msg['Subject'] = 'RAL Students Secret Santa!'
msg['From'] = "Samuel Jackson"
msg['To'] = reciever
server.sendmail(configData["email"], [reciever], msg.as_string())
print 'Message sent to ' + reciever
server.quit()
#randomly shuffled list of names
gifters, emailsAddresses = loadData('names.txt')
#make dictionary of emails and names
emailsAddresses = dict(zip(gifters, emailsAddresses))
random.shuffle(gifters)
#rotate list by one
#this ensures nobody gets themselves
recievers = list(gifters)
recievers = rotate(recievers, 1)
santaList = dict(zip(gifters, recievers))
#build body of email message
messages = [gifter + ' you are the secret santa for ' + reciever
for gifter, reciever in santaList.iteritems()]
#make list of messages and recipients
emails = zip([emailsAddresses[gifter] for gifter, reciever in santaList.iteritems()], messages)
#send the emails!
try:
sendMail(emails)
except smtplib.SMTPException, e:
print "Oh No! Unexpected STMP exception: ", e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment