-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecret_santa.py
116 lines (79 loc) · 2.93 KB
/
secret_santa.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import smtplib
from string import Template
import json
import random
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
CREDENTIALS = 'credentials.json'
CONTACTS = 'emails.txt'
MESSAGE_TEMPLATE_FILE = 'message.txt'
MESSAGE_SUBJECT = 'Test'
# Function to read the credentials file
def parse_json(filename):
with open(filename, mode='r') as json_file:
data = json.load(json_file)
SMTP_SERVER = data['SMTPserver']
EMAIL = data['email']
PASS = data['pass']
return SMTP_SERVER, EMAIL, PASS
# Function to read the emails from a file
def get_contacts(filename):
names = []
players = {}
with open(filename, mode='r', encoding='utf-8') as contacts_file:
# Iterate over the lines of the file and split each one by a whitespace
for contact in contacts_file:
name, email = contact.split()
# Store the data
names.append(name)
players[name] = email
return names, players
# Function to read a template file and return a template object made from its contents
def read_template(filename):
with open(filename, mode='r', encoding='utf8') as template_file:
template_file_content = template_file.read()
return Template(template_file_content)
# Setting up the SMTP server
def smtp_setup(SMTP_SERVER, EMAIL, PASS):
s = smtplib.SMTP(host=SMTP_SERVER, port='587')
s.starttls()
s.login(EMAIL, PASS)
return s
# Write the message
def write_send_messages(players, result, subject, smtp):
message_template = read_template(MESSAGE_TEMPLATE_FILE)
# Write and send the messages
for buyer, receiver in result.items():
# Cretae a message
msg = MIMEMultipart()
# Person name for the message template
message = message_template.substitute(PERSON_BUYING_GIFT=buyer, PERSON_RECEIVING_GIFT=receiver)
# Set up the parameters of the message
msg['From'] = players.get(receiver)
msg['To'] = players.get(buyer)
msg['Subject'] = subject
# Message body
msg.attach(MIMEText(message, 'plain'))
# Send the message via the server
smtp.send_message(msg)
del msg
# Function that shuffles the players and assigns secret santas
def shuffle_players(names):
return {names[i]:names[(i+1) % len(names)] for i in range(len(names))}
def main():
# Get JSON information
SMTP_SERVER, EMAIL, PASS = parse_json(CREDENTIALS)
# Setting up the SMTP server
smtp = smtp_setup(SMTP_SERVER, EMAIL, PASS)
# Obtain names and emails
names, players = get_contacts(CONTACTS)
# Shuffle players
result = shuffle_players(names)
print(result)
# Write and send the messages
write_send_messages(players, result, MESSAGE_SUBJECT, smtp)
# Terminate the SMTP connection and close it
smtp.quit()
print("Secret Santas has been sent. Good luck!")
if __name__ == '__main__':
main()