Compare commits
5 commits
a763c653bd
...
7732db81d9
Author | SHA1 | Date | |
---|---|---|---|
7732db81d9 | |||
fb0f8e1777 | |||
1bea9f70c3 | |||
b61d1dd8e2 | |||
fd0e30bca2 |
3 changed files with 115 additions and 69 deletions
|
@ -3,5 +3,6 @@
|
||||||
"port":587,
|
"port":587,
|
||||||
"user":"you@yourmail.com",
|
"user":"you@yourmail.com",
|
||||||
"pass":"yourPassword",
|
"pass":"yourPassword",
|
||||||
"sender":"santa@yourmail.com"
|
"sender":"santa@yourmail.com",
|
||||||
|
"price": "75€"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
[
|
[
|
||||||
{"name":"Alice","mail":"alice@mail.com","gender":"e"},{"name":"Bob","mail":"bob@mail.com","gender":"er"},{"name":"Chris","mail":"chris@mail.com","gender":"er"}
|
{"name":"Alice","mail":"alice@stefansterz.com","gender":"e"},
|
||||||
|
{"name":"Bob","mail":"bob@stefansterz.com","gender":"er"},
|
||||||
|
{"name":"Chris","mail":"chris@stefansterz.com","gender":"er"}
|
||||||
]
|
]
|
||||||
|
|
97
wichteln.py
Normal file → Executable file
97
wichteln.py
Normal file → Executable file
|
@ -1,3 +1,6 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
'''
|
'''
|
||||||
This script creates randoms pairs from a list of participants in a secret santa
|
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
|
game and sends them an email to notify them who the person is that they should
|
||||||
|
@ -8,28 +11,15 @@ buy presents for.
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
from email.utils import formatdate
|
from email.utils import formatdate, make_msgid
|
||||||
import json
|
import json
|
||||||
import random
|
import random
|
||||||
import smtplib
|
import smtplib
|
||||||
|
from typing import Literal
|
||||||
|
import types
|
||||||
|
|
||||||
parser = ArgumentParser()
|
|
||||||
parser.add_argument('-p', '--participants',
|
|
||||||
dest='pFileLocation',
|
|
||||||
default='participants.json',
|
|
||||||
help='read a list of participants from PARTICIPANTFILE',
|
|
||||||
metavar='PARTICIPANTFILE')
|
|
||||||
parser.add_argument('-c', '--config',
|
|
||||||
dest='cFileLocation',
|
|
||||||
default='config.json',
|
|
||||||
help='read the configuration from CONFIGFILE',
|
|
||||||
metavar='CONFIGFILE')
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
# load and prepare a list containing all participants
|
|
||||||
with open(args.pFileLocation, 'r') as pFile:
|
|
||||||
participants = json.load(pFile)
|
|
||||||
|
|
||||||
|
def secret_santa_matching(participants: list) -> list:
|
||||||
# create a copy of the list to make choosing a partner easier
|
# create a copy of the list to make choosing a partner easier
|
||||||
copy = list(enumerate(participants[:]))
|
copy = list(enumerate(participants[:]))
|
||||||
|
|
||||||
|
@ -55,13 +45,15 @@ for i in range(len(participants)):
|
||||||
while partner[0] == i:
|
while partner[0] == i:
|
||||||
partner = random.choice(copy)
|
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)
|
copy.remove(partner)
|
||||||
|
|
||||||
# load mail server configuration and open smtp server connection
|
return participants
|
||||||
with open(args.cFileLocation, 'r') as cFile:
|
|
||||||
config = json.load(cFile)
|
|
||||||
|
|
||||||
|
|
||||||
|
def send_secret_santa_mails(participants: list, config: dict):
|
||||||
|
open smtp connection
|
||||||
server = smtplib.SMTP(config['server'], config['port'])
|
server = smtplib.SMTP(config['server'], config['port'])
|
||||||
server.ehlo()
|
server.ehlo()
|
||||||
server.starttls()
|
server.starttls()
|
||||||
|
@ -71,17 +63,68 @@ server.login(config['user'], config['pass'])
|
||||||
# create emails for all participants and send them
|
# create emails for all participants and send them
|
||||||
# change the text for the mail here if you want to
|
# change the text for the mail here if you want to
|
||||||
for r in participants:
|
for r in participants:
|
||||||
|
|
||||||
msg = MIMEMultipart('alternative')
|
msg = MIMEMultipart('alternative')
|
||||||
msg['From'] = config['sender']
|
msg['From'] = config['sender']
|
||||||
msg['To'] = r['mail']
|
msg['To'] = r['mail']
|
||||||
msg['Subject'] = 'Wichteln 🎄'
|
msg['Subject'] = 'Wichteln 🎄'
|
||||||
|
msg['Message-ID'] = make_msgid(domain=config['sender']
|
||||||
|
.split('@')[1]
|
||||||
|
.strip('>'))
|
||||||
msg['Date'] = formatdate(localtime=True)
|
msg['Date'] = formatdate(localtime=True)
|
||||||
|
|
||||||
body = 'Lieb'+r['gender']+' '+r['name']+'!\n\n'
|
plain = f'''Lieb{r['gender']} {r['name']}!
|
||||||
body += 'Du bist heuer Wichtel für '+r['partner']+'.\n'
|
|
||||||
body += 'Das Geschenk sollte nicht mehr als 50€ kosten.\n\n'
|
|
||||||
body += 'Frohe Weihnachten!\n— Dein Christkind 👼'
|
|
||||||
|
|
||||||
msg.attach(MIMEText(body, 'plain', 'utf-8'))
|
Du bist heuer Wichtel für {r['partner']}.
|
||||||
|
Das Geschenk sollte nicht mehr als {config['price']} kosten.
|
||||||
|
|
||||||
|
Frohe Weihnachten!
|
||||||
|
— Dein Christkind 👼'''
|
||||||
|
|
||||||
|
html = f'''
|
||||||
|
<html>
|
||||||
|
<head></head>
|
||||||
|
<body>
|
||||||
|
<p>Lieb{r['gender']} {r['name']}!<br><br>
|
||||||
|
Du bist heuer Wichtel für {r['partner']}.<br>
|
||||||
|
Das Geschenk sollte nicht mehr als {config['price']} kosten.<br><br>
|
||||||
|
Frohe Weihnachten!<br>
|
||||||
|
— Dein Christkind 👼
|
||||||
|
</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
'''
|
||||||
|
|
||||||
|
msg.attach(MIMEText(plain, 'plain', 'utf-8'))
|
||||||
|
msg.attach(MIMEText(html, 'html', 'utf-8'))
|
||||||
|
print('Sending email to', r['name'], '...')
|
||||||
server.sendmail(config['sender'], r['mail'], msg.as_string())
|
server.sendmail(config['sender'], r['mail'], msg.as_string())
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = ArgumentParser()
|
||||||
|
parser.add_argument('-p', '--participants',
|
||||||
|
dest='pFileLocation',
|
||||||
|
default='participants.json',
|
||||||
|
help='read list of participants from PARTICIPANTFILE',
|
||||||
|
metavar='PARTICIPANTFILE')
|
||||||
|
parser.add_argument('-c', '--config',
|
||||||
|
dest='cFileLocation',
|
||||||
|
default='config.json',
|
||||||
|
help='read the configuration from CONFIGFILE',
|
||||||
|
metavar='CONFIGFILE')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
matches = []
|
||||||
|
|
||||||
|
# load and prepare a list containing all participants
|
||||||
|
with open(args.pFileLocation, 'r') as pFile:
|
||||||
|
matches = secret_santa_matching(json.load(pFile))
|
||||||
|
|
||||||
|
# load mail server configuration
|
||||||
|
with open(args.cFileLocation, 'r') as cFile:
|
||||||
|
config = json.load(cFile)
|
||||||
|
send_secret_santa_mails(matches, config)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in a new issue