lunes, 22 de diciembre de 2014

Nerdy Secret Santa

Just today I had to organize my family's secret santa. Seemed pretty simple, but after googling for a while I just couldn't find any straight forward way of 1) assigning secret santas and 2) sending the damn emails. Everything looked so spaceshippy.

So I built my own in under 5 minutes. Merry xmas!


 import random  
 import smtplib  
   
   
 # Change message accordingly  
 # Please note I worship the dark lord.  
 msg = """From: Secret Satan <noreply@secretsatan.com>  
 To: My Friends <%s>  
 Subject: Super Nerdy Secret Satan  
   
 Your secret satan is: %s  
   
 Budget is $20.  
   
 Ps. copy, paste & test is best!  
 """  
   
 # the following list should contain emails  
 # i.e., ['email1@foo.com', ...]  
 emails = []  
   
 # build a list of tuples containing (receiver@email.com, his.secret@satan.com) pairs  
 # odd groups will have a lucky winner getting two gifts! yay.  
 while True:  
   couples = zip(emails, random.sample(emails, len(emails)))  
   if not True in [i==j for i,j in couples]:  
     break  
   
 # connect & send. I use webfaction  
 EMAIL_HOST = 'smtp.webfaction.com'  
 EMAIL_HOST_USER = 'myuser'  
 EMAIL_HOST_PASSWORD = 'mypass'  
 from_addr = 'noreply@secretsatan.com'  
 smtpobj = smtplib.SMTP()  
 smtpobj.connect(EMAIL_HOST)  
 smtpobj.login(EMAIL_HOST_USER, EMAIL_HOST_PASSWORD)  
   
 # for really long lists of emails you should keep track of sent messages  
 # or do some sort of commit/rollback  
 for c in couples:  
   smtpobj.sendmail(from_addr, [c[0]], msg % (c[0], c[1]))  
   
 smtpobj.quit()  

No hay comentarios:

Publicar un comentario