From 0d1c1878b1c322e366b053b7660380811b918e8b Mon Sep 17 00:00:00 2001 From: Stefan Sterz Date: Sat, 8 Sep 2018 21:02:29 +0000 Subject: [PATCH] feat: implement email and argument handeling --- config.json | 7 ++++++ participants.json | 3 +++ wichteln.py | 57 +++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 config.json create mode 100644 participants.json diff --git a/config.json b/config.json new file mode 100644 index 0000000..9514eaa --- /dev/null +++ b/config.json @@ -0,0 +1,7 @@ +{ +"server":"your.email.server", +"port":587, +"user":"you@yourmail.com", +"pass":"yourPassword", +"sender":"santa@yourmail.com" +} diff --git a/participants.json b/participants.json new file mode 100644 index 0000000..1c9180b --- /dev/null +++ b/participants.json @@ -0,0 +1,3 @@ +[ +{"name":"Alice","mail":"alice@mail.com","gender":"e"},{"name":"Bob","mail":"bob@mail.com","gender":"er"},{"name":"Chris","mail":"chris@mail.com","gender":"er"} +] diff --git a/wichteln.py b/wichteln.py index 50f61cb..ad8f1ae 100644 --- a/wichteln.py +++ b/wichteln.py @@ -1,8 +1,33 @@ +''' +This script creates randoms pairs from a list of participants in a secret santa +game and sends them an email to notify them who the person is that they should +buy presents for. +''' + +# -*- encoding: utf-8 -*- +from argparse import ArgumentParser +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText import json import random +import smtplib + +parser = ArgumentParser() +parser.add_argument('-p', '--participants', + dest='pFileLocation', + default='participants.json', + help='read list of participants from FILE', + metavar='PARTICIPANTFILE') +parser.add_argument('-c', '--config', + dest='cFileLocation', + default='config.json', + help='read the configuration from FILE', + metavar='CONFIGFILE') + +args = parser.parse_args() # load and prepare a list containing all participants -with open('participants.json', 'r') as pFile: +with open(args.pFileLocation, 'r') as pFile: participants = json.load(pFile) # create a copy of the list to make choosing a partner easier @@ -20,7 +45,7 @@ for i in range(len(participants)): partner = random.choice(participants) current['partner'] = partner['partner'] - partner['partner'] = current['name'] + ' — ' + current['mail'] + partner['partner'] = current['name'] + ' (' + current['mail']+')' participants.append(current) break @@ -30,8 +55,32 @@ for i in range(len(participants)): while partner[0] == i: partner = random.choice(copy) - participants[i]['partner'] = partner[1]['name']+' — '+partner[1]['mail'] + participants[i]['partner'] = partner[1]['name']+' ('+partner[1]['mail']+')' copy.remove(partner) -print(participants) +# load mail server configuration and open smtp server connection +with open(args.cFileLocation, 'r') as cFile: + config = json.load(cFile) + +server = smtplib.SMTP(config['server'], config['port']) +server.ehlo() +server.starttls() +server.ehlo() +server.login(config['user'], config['pass']) +# create emails for all participants and send them +# change the text for the mail here if you want to +for r in participants: + + msg = MIMEMultipart('alternative') + msg['From'] = config['sender'] + msg['To'] = r['mail'] + msg['Subject'] = 'Wichteln 🎄' + + body = 'Lieb'+r['gender']+' '+r['name']+'!' + body += '\n\nDu bist heuer Wichtel für '+r['partner'] + body += '. \n Das Geschenk sollte nicht mehr als 50€ kosten.' + body += '\nFrohe Weihnachten!\n\nDein Christkind 👼' + + msg.attach(MIMEText(body, 'plain', 'utf-8')) + server.sendmail(config['sender'], r['mail'], msg.as_string())