-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailer.py
executable file
·103 lines (78 loc) · 3.46 KB
/
mailer.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
#!/usr/bin/python
"""
This program is a simple template driven mass emailer.
A sample query to generate a list of the user of the cloud.
SELECT rcuser.email, rcuser.displayname FROM rcshibboleth.user AS rcuser LEFT JOIN keystone.user AS kuser ON rcuser.user_id = kuser.id WHERE kuser.enabled = 1 GROUP BY rcuser.user_id INTO OUTFILE '/tmp/contacts.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
"""
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
import argparse
import csv
import os
import sys
import smtplib
import jinja2
DEBUG = 0
TEMPLATE_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'templates')
templateLoader = jinja2.FileSystemLoader(searchpath=TEMPLATE_DIR)
templateEnv = jinja2.Environment(loader=templateLoader)
def collect_args():
parser = argparse.ArgumentParser(description='Email Announcements.')
parser.add_argument('--test', action='store_true', default=False,
help='send all emails to the test email account.')
parser.add_argument('--test-template', action='store_true', default=False,
help='print the emails but don\'t send them')
parser.add_argument('--test-email', nargs='?',
default=os.environ.get("MAIL", ""),
help='The email address to send to by default.')
parser.add_argument('--users', nargs='?',
required=True,
help='A CSV file with a list of the users.')
parser.add_argument('--template', nargs='?',
required=True,
help='The template to send to the user.')
parser.add_argument('--subject', nargs='?',
required=True,
help='The subject of the email.')
parser.add_argument('-p', '--smtp_server',
default='127.0.0.1',)
parser.add_argument('-v', '--verbose', action='count', default=0)
return parser
def send_email(recipient, subject, text, html=None, print_only=False):
global smtp_server
msg = MIMEMultipart('alternative')
msg.attach(MIMEText(text, 'plain', 'utf-8'))
if html is not None:
msg.attach(MIMEText(html, 'html', 'utf-8'))
msg['From'] = 'NeCTAR Research Cloud <[email protected]>'
msg['To'] = recipient
msg['Reply-to'] = '[email protected]'
msg['Subject'] = subject
if print_only:
print '\n\n\n\n\n', msg
return
else:
print 'Sending email to:', recipient
s = smtplib.SMTP(smtp_server)
s.set_debuglevel(DEBUG)
try:
s.sendmail(msg['From'], [recipient], msg.as_string())
except smtplib.SMTPRecipientsRefused as err:
sys.stderr.write('%s\n' % str(err))
finally:
s.quit()
if __name__ == "__main__":
args = collect_args().parse_args()
smtp_server = args.smtp_server
template = templateEnv.get_template(args.template)
sent_addresses = set()
with open(args.users) as csvfile:
for user in csv.DictReader(csvfile, fieldnames=['email', 'name']):
if user['email'] in sent_addresses:
print "Skipping duplicate:", user['email']
continue
sent_addresses.add(user['email'])
to_address = user['email'] if not args.test else args.test_email
content = template.render(user)
send_email(to_address, args.subject, content,
print_only=args.test_template)