Secret_Santa.py
December 03 2016

##########################
###    SECRET SANTA    ###
###  Bradism.com 2016  ###
##########################
# Set Values Below:
SMTP_DETAILS = {  # Available from your Webhost or ISP
   'SMTPserver': 'your.smtp.com',
   'sender': 'YOUR_EMAIL_ADDRESS',
   'username': 'SMTP_USER_NAME',
   'password': 'YOUR_SMTP_PASSWORD'
}

# Create a dictionary of participants, adding their name, email address and a description of what they might want/not want.
# The "gift" field will be included as HTML, so all HTML tags are valid and make sure to escape double-quotes
giftees = {}
giftees['Brad'] = {'email': 'brad@example.com', 'gift': "Yogurt, Cereal, Fruit.<br />No cats!"}
giftees['Dwight'] = {'email': 'dwight@atlanta.com', 'gift': "I'd like to shoot more threes and have someone yell \"Dwight for THREEEE\" even if it looks like missing."}
giftees['Rajon'] = {'email': 'big_game_rondo@gmail.com', 'gift': "<ol><li>Connect 4 Opponents</li><li>A call from Paul Pierce</li></ol>"}
giftees['Gortat'] = {'email': 'marcin@hotmail.com.com', 'gift': "I miss Stan Van Gundy. Also, shaving products."}
 
#######################################################
### Custom Settings Finished                        ###
### Shouldn't need to edit anything below this line ###
#######################################################

# Mail Subroutine
def send_mail ( destination_addr, addressee_name, target_name, target_requests, SMTP_DETAILS ): 
    import sys
    import os
    import re

    sender =     SMTP_DETAILS['sender']
    destination = [ destination_addr ]

    subject="Hi %s - Here's Your Secret Santa" % addressee_name
    content="<p>Hi %s</p><p>Your Secret Santa is <b>%s!</b></p><p>Their Likes/Requests are:</p><p>%s</p><p>Good Luck and remember, keep it a secret!</p><p>Love, Santa</p>" % (addressee_name, target_name, target_requests)
    
    from smtplib import SMTP_SSL as SMTP       # this invokes the secure SMTP protocol (port 465, uses SSL)
    # old version
    from email.mime.text import MIMEText

    try:
        msg = MIMEText(content, 'html')
        msg['Subject']= subject
        msg['From']   = sender # some SMTP servers will do this automatically, not all
        
        conn = SMTP(SMTP_DETAILS['SMTPserver'])
        conn.set_debuglevel(False)
        conn.login( SMTP_DETAILS['username'], SMTP_DETAILS['password'] )
        try:
            conn.sendmail(sender, destination_addr, msg.as_string())
        finally:
            conn.quit()

    except Exception, exc:
        sys.exit( "mail failed; %s" % str(exc) ) # give a error message

# Recursive People Shuffler        
def shuffle_people ( inarray, people ):
    import random
    random.shuffle(inarray)
        
    valid = 1    
    for i in range(0, len(inarray)):
        if inarray[i] == people[i]:
            valid = 0
    
    if valid == 1:
        return inarray
    else:
        return shuffle_people(inarray, people)

# MAIN        
import copy
print "Running Secret Santa"
people = giftees.keys()

pre_shuffled_people = copy.copy(people)
shuffled_people = shuffle_people(pre_shuffled_people, people)

for i in range(0, len(people)):
    name = people[i]
    target_name = shuffled_people[i]
    email = giftees[name]['email']
    target_request = giftees[target_name]['gift']
       
    send_mail( email, name, target_name, target_request, SMTP_DETAILS)
 
print "Secret Santa Complete"

Viewed 98 times

This Article has not been rated yet

 0 COMMENTS:

No Comments