Created
November 21, 2018 18:00
-
-
Save IT102Gists/e4549f096f840e671b82af5aa852e8a9 to your computer and use it in GitHub Desktop.
Remove the hassle from making Secret Santa gift giving assignments with Python 3. This short program uses Standard Library modules like csv, random, and smtplib to read a file, create random Secret Santa assignments, then prepare & send email notifications to each participant.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# To start, set up a Google form that collects | |
# first name, last name, and email address from participants, | |
# then export as a CSV file and store it in the same | |
# directory as this program | |
# also, enable less secure apps in gmail before running | |
# https://support.google.com/accounts/answer/6010255?hl=en | |
import csv | |
import smtplib | |
from random import shuffle | |
from email.mime.text import MIMEText | |
# function we'll use later on for sending notifications | |
def send_gmail(from_addr, password, to_addr, subject, body): | |
"""prepares and sends gmail""" | |
msg = MIMEText(body) | |
msg["Subject"] = subject | |
msg["From"] = from_addr | |
msg["To"] = to_addr | |
s = smtplib.SMTP("smtp.gmail.com: 587") | |
s.ehlo() | |
s.starttls() | |
s.login(from_addr, password) | |
s.send_message(msg) | |
s.quit() | |
# create empty containers | |
participants= list() | |
pairs = dict() | |
# read through imported participant CSV file | |
# row[0] = fname, row[1] = lname, row[2] = email address | |
with open("names.csv", "r") as f: | |
csv_reader = csv.reader(f) | |
for row in csv_reader: | |
participants.append((row[0].strip() + " " | |
+ row[1].strip(), row[2].strip())) #(fname lname, email) | |
shuffle(participants) # participants is a list of tuples, fyi | |
# iterate over shuffled participants to create key, value pairs | |
for i in range(len(participants)): | |
pairs[participants[i]] = participants[(i+1) % len(participants)] | |
# write Secret Santa pairings to new CSV file for future reference | |
with open("match_list.csv", "w", newline="") as f: | |
csv_writer = csv.writer(f) | |
for key,value in pairs.items(): | |
csv_writer.writerow([key[0], key[1],"---------->", value[0], value[1]]) | |
# send email notification to each Secret Santa participant | |
for key, value in pairs.items(): | |
key_first_name = key.split()[0] | |
body = f"Hi {key_first_name}!\n\n" \ | |
f"Thank you for participating in this year's gift exchange. " \ | |
f"I have randomly assigned you to give a gift to:\n\n" \ | |
f"{value[0]}\n{value[1]}\n\n" \ | |
f"I recommend spending no more than $10. " \ | |
f"Thanks again for participating, and happy holidays!\n\n" \ | |
f"Cheers,\n\n" \ | |
f"(your name)" | |
try: | |
send_gmail("#your gmail", "#your password", | |
key[1], "It's Secret Santa Time!", body) | |
except Exception as e: | |
print(f"There was a problem sending an email to: {value[1]}") | |
print(e) | |
print("\n***\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment