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 <>  
 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., ['', ...]  
 emails = []  
 # build a list of tuples containing (, 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]:  
 # connect & send. I use webfaction  
 EMAIL_HOST = ''  
 EMAIL_HOST_USER = 'myuser'  
 from_addr = ''  
 smtpobj = smtplib.SMTP()  
 # 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]))  

No hay comentarios:

Publicar un comentario