Automated Emailing
Last Christmas, I was organizing a Secret Santa activity for students in my high school’s book club. This activity assigns every student as someone else’s Secret Santa. Each Santa would then choose a book as a present to the person they were assigned. Thus, every student would receive a book suggestion from a secret member of the club. The full rules of Secret Santa can be found here: https://www.elfster.com/secret-santa-rules/.

The problem was assigning Secret Santas. We could not meet in person due to COVID-19, so we could not randomize the Secret Santas with a hat draw. However, if I had was to assign them myself, I could not join in on the game, because I would have to assign my own Secret Santa. Therefore, I programmed a Python program to help me assign Secret Santas to each student.
My code had two major parts: the algorithm to assign Secret Santas, and the sending of emails through Python. The email would tell each person who they are the Secret Santa for. In other words, they would be sent the person they should choose a book for.
Assigning Secret Santas
My requirements to assign Secret Santas was to 1) randomize the assignments and 2) make sure no one is assigned to themselves. To do this, I used the random library and pandas library to organize the participants.
import random
import pandas as pd#Participants.xlsx is an excel file of names to emails
file = pd.read_excel('Participants.xlsx')
names = file["Name"]
emails = file["Email"]
With a list of names and emails from the Participants excel file, I created a dictionary to map people’s emails to their names. This dictionary would look like this:
{“Student A”: “Email A”,
“Student B”: “Email B”,
“Student C”: “Email C”, …}
data = {}
for i in range(len(names)):
data[names[i]] = emails[i]
The next block of code is the algorithm that allowed me to randomly assign Secret Santas based on the data. This information was stored in a dictionary that would email a student the person they need to send a book to.

For example, if assignment looked like this:
{“Email A”: “Student C”,
“Email B”: “Student A”,
“Email C”: “Student B”…}
then the program would tell Student A (Email A) to choose a book for Student C. And send Student B (Email B) an email to choose a book for Student A, etc.
assignments = {}
length = len(data)
for n in range(length):
index = random.randint(0, length-1)
person = names[index]
assignments[data[names[n]]] = person
while names[n] == assignments[data[names[n]]]:
index = random.randint(0, length-1)
person = names[index]
assignments[data[names[n]]] = person
del data[names[n]]
length = len(data)
With the assignments decided, it’s time to send the emails.
Sending Emails — Automated
In order for my program to send automated emails through Python, I used the smtplib module. Here is a more detailed article explaining the steps to sending emails with Python: https://realpython.com/python-send-email/.
import smtplib, ssl
port = 465 #For SSL
smtp_server = "smtp.gmail.com"organizer_email = "email" #Your gmail
organizer_password = "password" #Your gmail password
for email in assignments:
message = """\
#message content sent to each student Subject: Secret Santa Assignment
The person you should send a book to for Secret Santa is """ +
assignments[email]
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as
server:
server.login(organizer_email, organizer_password)
server.sendmail(organizer_email, email, message)
Summary

Using a Python algorithm, I was able to randomize assignments for a Secret Santa activity. All I needed to have was a spreadsheet of participants and their emails. The program is then able to automatically send each participant an email containing the person they were assigned. This program allowed me, the organizer, to be able to join the game as well.