Secret Santa by Email

Divyashie Soopal
3 min readJan 3, 2023

--

Ref: [Fun blog]

During the holiday season, my team decided we will gift each other a present. However, we wanted it to be more fun. A secret. Closer to the concept of Secret Santa.

Traditionally, to execute our Secret Santa project, we would write names on a piece of paper, fold them, keep it in a big box, shuffle the papers, and then generate a name/the chosen person here. But, there is a catch. What if the same person gets his/her own name? We could not afford that.

We wanted a more efficient, time-saving, error-free system, but also quick to develop. What if each person gets emailed their respective wish for whom they will act as Santa? This whole process will be automated using a simple logic of name shuffling.

Also, to address the issue of name repetition mentioned above, I simply created a duplicate store for the names and remove those that were already assigned their (wish). This code assumes that we have an even list of people. If we wanted to do a one-to-many relationship, that is, one Santa many kids we would have to slightly re-write this logic.

def santaKid(team_name): 
"""
generates a list of dictionary containing unique kid to a santa.
"""
copy_team = team_name.copy()
result = []
for santa in team_name:
kid = random.choice(copy_team)
while santa == kid:
kid = random.choice(copy_team)
result.append({santa: kid})
copy_team.remove(kid)
return result

The next step was to create a stylish Christmas-themed email. For that, I used HTML to style the content of the email as follows. The message is written in French — the language used by the Company I work for.

def emailContent(santa, kid): 
message = f''''
<!DOCTYPE html>
<html>
<body>
<div style="background-color:#ff7f50;padding:10px 20px;">
<h2 style="font-family:'Mountains of Christmas', cursive;color#ffa07a; text-align:center;"> Echange des cadeaux chez Apsim</h2>
</div>
<div style="padding:20px 0px">
<div>
<img src="https://www.dukeshillham.co.uk/Portals/0/product/images/secret_satnta_2019.jpg" style="width:100%;">
<div style="text-align:center;">
<h3>{santa}</h3>
<p>Ho Ho Ho!! </p>
<p> Histoire de faire perdurer l'esprit de noël dans nos bureaux, tu es invité à participer à l'échange des cadeaux.</p>
<p> L'elfe de noël t'a choisi pour être le secret Santa de <b>{kid}</b>! {kid} a été bon enfant et attend son cadeau avec patience.</p>
<img src= "https://clipground.com/images/christmas-box-clipart-13.jpg" style="height:300px;">
</div>
</div>
</div>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Mountains+of+Christmas:wght@700&display=swap" rel="stylesheet">
</body>
</html>
'''
return message

The Email’s display can be viewed here: http://tpcg.io/_IAX4H9.

Finally, to automate sending out the Email, I used the smtplib. This is a neat example of it:

def automateEmail(recipient,message): 
msg = EmailMessage()
msg['Subject'] = 'Mission: Santa Secret 🎅'
msg['From'] = sender
msg['To'] = recipient
msg.set_content(message, subtype='html')

try:
smtp = smtplib.SMTP("smtp-mail.outlook.com", port=587)
smtp.starttls()
smtp.login(sender, password)
smtp.send_message(msg)
smtp.quit()
print("Email sent successfully!")
except Exception as e:
print(e)

Please note that for Outlook the port is 587 but it will be different for other e-mail providers.

The next time you need to conduct a draw company-wide, you will try to dev it ;)

Reference:

https://github.com/Apsim-Ltd/secret_santa.git

https://www.fun.com/blog/p-1252-whos-your-secret-santa.aspx

--

--

Divyashie Soopal
Divyashie Soopal

Written by Divyashie Soopal

Passionate about learning tech, hoping to contribute to the community.

No responses yet