The Problem
This Christmas season I found myself running a secret santa, completely over the internet. I want to keep everyones secret santa information secret, without even me knowing it. I also wanted to automate the emailing process, which was required to keep the information for who had who, away from me. A log file also needed to be produced (and promised not to be looked at... or encrypted), to help fix disputes. So I ended up with the following requirements:
- Takes a list of participants and generates secret santa pairs.
- Create a message from a template
- Send the email
- Create a log file
The Solution
I am not going to lie, this piece of kit was not particularly difficult to write, but I believe is a good argument for why everyone should know how to write scripts; and why public API's and libraries are awesome.
To write the script I chose Python, which has a built in JSON parser library, and support for SendGrid's API. I chose SendGrid to send the emails because it required the least setup, I just needed to set up an account and I could send emails. Their API was simple and all of the code I used for interacting with it was ripped directly out of their examples. Quick and easy, just what I wanted.
I then came up with a quick JSON structure for containing all of the information I needed, an example of which can be found below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
{ "sendgrid_info": { "uname": "SENDGRID_USERNAME", "pwd": "SENDGRID_PASSWORD" }, "santas": [ { "name": "Name 1", }, { "name": "Name 2", }, { "name": "Name 3", }, { "name": "Name 4", }, ], "subject": "Example", "message": "<p>Hello {0},</p><p>You are the secret santa of <b>{1}</b></p>" } |
The python script parses the JSON file, shuffles the participants, links them together into partners and then sends each of the participants an email populating the template sotred in "message". I will admit this script has a lot of short comings, but it successfully helped me send out my secret santa emails and everyone is happy!
You can access the code on Github.