Skip to content

Instantly share code, notes, and snippets.

@marshareb
Created November 29, 2017 18:59
Show Gist options
  • Save marshareb/badaab39a004471a3ed575a565b81ab0 to your computer and use it in GitHub Desktop.
Save marshareb/badaab39a004471a3ed575a565b81ab0 to your computer and use it in GitHub Desktop.
Emails people secret santa info
import smtplib
import random
from email.message import EmailMessage
# PRIVATE EMAIL INFO
# (This is your email. Note this is not secure, so don't do this on public networks.)
gmail_sender = 'sender@gmail.com'
gmail_passwd = 'password'
def email_user(secret_santa_email, person, items):
# Sign into email
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.login(gmail_sender, gmail_passwd)
except:
print("Error signing into email")
# Send email
msg = "You received " + str(items[person]) + " as your Secret Santa! Their email is " + str(person) + "."
server.sendmail(gmail_sender, secret_santa_email, msg)
server.close()
def select_random_person(i, lis):
x = random.choice(lis)
while x == i:
x = random.choice(lis)
y = lis.index(x)
lis.pop(y)
return x
def write_dict(lis, items):
f = open("secret_santa.txt", "w+")
for i in lis:
f.write(str(items[i[0]]) + " : " + str(items[i[1]]) +"\n")
f.close()
# Email -> name
emails = {'email1@email.com' : 'name1', 'email2@email.com' : 'name2'}
flis = [i[0] for i in emails.items()]
slis = [i for i in flis]
# Holds the tuples of emails
tlis = list(map(lambda x: (x, select_random_person(x, slis)), flis))
# Emails each person in the list
list(map(lambda x: email_user(x[0], x[1], emails), tlis))
# Output a text file with each corresponding person
write_dict(tlis, items)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment