Created
June 2, 2016 20:24
-
-
Save gothma/464ae7f3857bac2b3b05f185457f44ca to your computer and use it in GitHub Desktop.
Script to assign secret santas and notify them using smtp
This file contains hidden or 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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# Import smtplib for the actual sending function | |
import smtplib | |
import getpass | |
from email.mime.text import MIMEText | |
from random import Random | |
# Names and mail adresses of people that a part of the Wichtel ceremony | |
mails = { | |
"Alice": "[email protected]", | |
"Bob": "[email protected]", | |
"Foo": "[email protected]", | |
"Bar": "[email protected]" | |
} | |
names = list(mails.keys()) | |
# E-Mail settings | |
smtpaddress = "[email protected]" | |
smtpusername = "[email protected]" | |
smtpserver = "smtp.example.com" | |
smtpport = 465 | |
# Shuffle the list | |
# Every participant is a wichtel to the next person in the shuffled list | |
# The last person in the list is the wichtel to the first one | |
oracle = Random() | |
wichtels = list(names) | |
targets = list(names) | |
oracle.shuffle(targets) | |
# Check for duplicates (wichtel who drew themselves) | |
swaps = list() | |
for (i, wichtel), target in zip(enumerate(wichtels), targets): | |
if wichtel is target: | |
swaps.append(i) | |
# Swap target for those who drew themselves | |
if len(swaps) is 1: # With a random other | |
i = swaps[0] | |
j = (i + 1) % len(names) | |
targets[i] = targets[j] | |
targets[j] = wichtels[i] | |
else: # With the other duplicates | |
for i, s in enumerate(swaps): | |
targets[s] = wichtels[swaps[(i + 1) % len(swaps)]] | |
# Check Wichtels, just to be sure | |
for wichtel, target in zip(wichtels, targets): | |
assert(wichtel is not target) | |
# Connect to the mailserver | |
print("Establish connection to %s" % smtpserver) | |
smtp = smtplib.SMTP_SSL(smtpserver, smtpport) | |
smtp.login(smtpusername, getpass.getpass()) | |
print("Established") | |
# Sends a mail to the wichtel saying who it's target is | |
def sendwichtelmail(wichtel, target): | |
# Set the mail message | |
msg = MIMEText('''Hallo %s,\nDieses Jahr darfst du %s etwas schenken.\n | |
Grüße vom Christkind :)''' % (wichtel, target), "plain", "utf-8") | |
# Set the other attributes of the mail | |
msg['Subject'] = "[Geheim] Wichtel" | |
msg['From'] = "Christkind <" + smtpaddress + ">" | |
msg['To'] = wichtel + " <" + mails[wichtel] + ">" | |
# Send the message via our own SMTP server, but don't include the | |
# envelope header. | |
smtp.sendmail(smtpaddress, mails[wichtel], msg.as_string()) | |
# Actually send the mails | |
try: | |
for (i, name), target in zip(enumerate(wichtels), targets): | |
sendwichtelmail(name, target) | |
print("Sent mail %d/%d" % (i + 1, len(names))) | |
except: | |
# Close connection on error | |
smtp.quit() | |
raise | |
# Close mailserver connection | |
smtp.quit() | |
print("Connection closed") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment