Skip to content

Instantly share code, notes, and snippets.

@lao
Created January 18, 2024 12:31
Show Gist options
  • Save lao/a6cb285668dfb6ee81b395e3601f2fe8 to your computer and use it in GitHub Desktop.
Save lao/a6cb285668dfb6ee81b395e3601f2fe8 to your computer and use it in GitHub Desktop.
Tiny secret santa py
import datetime
import random
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def secret_santa(names):
"""Assigns a Secret Santa for each person in the list avoiding cycles of two."""
# Check for a valid number of participants
if len(names) < 2:
return "Need at least two participants."
shuffled_names = names[:]
random.shuffle(shuffled_names)
assignments = {}
for i in range(len(shuffled_names)):
if i == len(shuffled_names) - 1:
# The last person gets the first person in the list
assignments[shuffled_names[i]] = shuffled_names[0]
else:
# Everyone else gets the next person in the list
assignments[shuffled_names[i]] = shuffled_names[i + 1]
return assignments
def send_secret_santa_emails(assignments, email_info):
"""
Sends Secret Santa assignments to participants' emails.
:param assignments: A dictionary of Secret Santa assignments.
:param email_info: A dictionary containing email addresses and present preferences for each participant.
"""
# Email server settings
smtp_server = ""
smtp_port = 2525
sender_email = ""
sender_password = ""
# Connect to the email server
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(sender_email, sender_password)
date = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
for giver, receiver in assignments.items():
# Compose the email
msg = MIMEMultipart()
msg["From"] = sender_email
msg["To"] = email_info[giver]["email"]
msg["Subject"] = f"Hi {giver}! Here is your Secret Santa Assignment! @ {date}"
presents_str = ""
for present in email_info[receiver]["presents"]:
presents_str += f"{present}\n\n\n"
body = (f"Ho Ho Ho, dear {giver}!\n\n"
f"I'm Santa Claus, and I need your help this Christmas. I have so many gifts to deliver, "
"and I've chosen you to be a special helper for my Secret Santa mission.\n\n"
f"Your mission, should you choose to accept it, is to bring joy to {receiver}. "
f"I've learned that they would be thrilled to receive: {presents_str}. "
"Can I count on you to help spread the holiday cheer?\n\n"
"Remember, the spirit of Christmas lies in giving and sharing. Let's make this holiday season "
"a magical one for {receiver}.\n\n"
"Merry Christmas and Happy Gifting!\n\n"
"Santa Claus 🎅")
msg.attach(MIMEText(body, "plain"))
# Send the email
server.send_message(msg)
print(f"Message sent to {giver} @ {email_info[giver]['email']}")
# Disconnect from the server
server.quit()
# Example list of participants
participants_info = {
"User1": {
"email": "",
"presents": [
"https://www.amazon.es/-/pt/dp/B0BQ1JXKTX/ref=sr_1_6?crid=1U3L8HLIQ7S6C&keywords=airfryer&qid=1702831216&sprefix=air%2Caps%2C444&sr=8-6&th=1",
"https://www.amazon.es/-/pt/dp/B078RLK58F/ref=sr_1_1?crid=30RS882BX5A17&keywords=funda%2Bnordica%2B220x200&qid=1702831387&refinements=p_36%3A1323855031&rnid=1323854031&s=kitchen&sprefix=220x200%2B%2Caps%2C352&sr=1-1&th=1",
],
},
"User2": {
"email": "",
"presents": [
"https://www.amazon.com/dp/B07SRD2ZFF?ref_=cm_sw_r_apin_dp_A48NRHT8FQV4538402FX&language=en-US&th=1"
],
},
}
participants = list(participants_info.keys())
# print(participants)
# Run the Secret Santa assignment /
secret_santa_assignments = secret_santa(participants)
# print(secret_santa_assignments)
# Send the emails
send_secret_santa_emails(secret_santa_assignments, participants_info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment