forked from OSAlt/legacy-secret-santa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mass_mail.py
executable file
·111 lines (89 loc) · 3.28 KB
/
mass_mail.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
#!/usr/bin/env python
import argparse
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import random
import smtplib
import datetime
import socket
import markdown
import pytz
import time
import yaml
from santa_lib.person import Person
REQUIRED = ['SMTP_SERVER', 'SMTP_PORT', 'USERNAME', 'PASSWORD', 'TIMEZONE', 'PARTICIPANTS', 'FROM', 'SUBJECT']
def load_config(config_file):
try:
return yaml.load(open(config_file))
except:
print("Error, %s was not found" % config_file)
def load_participants(config):
"""
load all the participants from the configuration files and
:return a list of Person objects.
"""
people = []
for person in config['PARTICIPANTS']:
person = Person.construct_email_recipient(person)
if person is not None:
people.append(person)
return people
def send_emails(config, people):
server = smtplib.SMTP_SSL(config['SMTP_SERVER'], config['SMTP_PORT'])
server.login(config['USERNAME'], config['PASSWORD'])
for person in people:
msg = MIMEMultipart('alternative')
zone = pytz.timezone(config['TIMEZONE'])
now = zone.localize(datetime.datetime.now())
date = now.strftime('%a, %d %b %Y %T %Z') # Sun, 21 Dec 2008 06:25:23 +0000
message_id = '<%s@%s>' % (str(time.time()) + str(random.random()), socket.gethostname())
frm = config['FROM']
to = person.email # pair.giver.email
subject = config['SUBJECT']
template_file = config['TEMPLATE']
file_handle = open(template_file, 'r')
raw = file_handle.read()
message = markdown.markdown(raw)
body = message.format(
date=date,
message_id=message_id,
frm=frm,
to=to,
subject=subject,
person=person.name
)
if config['TEMPLATE_LOGO']:
image_url = config['TEMPLATE_IMAGE']
body += """<div align="center"><img alt="Bender Logo" src="{encoded}" /></div>""".format(
encoded=image_url)
msg['Subject'] = subject
msg['From'] = frm
msg['To'] = to
part1 = MIMEText(body, 'html')
msg.attach(part1)
server.sendmail(frm, [to], msg.as_string())
print "Email was sent to: {name} <{email}>.".format(name=person.name, email=person.email)
server.quit()
def main():
parser = argparse.ArgumentParser(description='Santa Mass Email script')
parser.add_argument('--send', action="store_true", dest="send", default=False, help="Enables Local Mode")
parser.add_argument('--config', action="store", dest="config", type=str,
help="Override default config file (config.yml)and specify your own yaml config",
default='mass_mail.yml')
args = parser.parse_args()
config_file = args.config
send = args.send
config = load_config(config_file)
if config is None:
return
for key in REQUIRED:
if key not in config.keys():
raise Exception(
'Required parameter %s not in yaml config file!' % (key,))
## Load participants
people = load_participants(config)
## send emails.
if send:
send_emails(config, people)
if __name__ == "__main__":
main()