mail-merge: allow sending to multiple emails
[user/alex/software/my-snippets.git] / mail-merge
1 #!/usr/bin/python
2
3 import csv
4 import email.parser
5 from optparse import OptionParser
6 import os
7 import smtplib
8 import subprocess
9 import sys
10
11
12 sender_header = 'mail-merge-sender@mit.edu'
13 rtkit_path = '/afs/athena.mit.edu/user/a/d/adehnert/arch/common/lib/python/'
14 smtp = None
15
16 def dictize_line(header, line,):
17     line_dict = {}
18     for key, elem in zip(header, line, ):
19         line_dict[key]=elem
20     return line_dict
21
22 def setup_sendmail_smtp():
23     global smtp
24     smtp = smtplib.SMTP()
25     smtp.connect()
26 def sendmail_smtp(addrs, text):
27     global smtp
28     smtp.sendmail(sender_header, addrs, text, )
29 smtp_funcs = (setup_sendmail_smtp, sendmail_smtp, )
30
31 def sendmail_cmd(addrs, text):
32     args = ["/usr/lib/sendmail", "--", ]
33     args.extend(addrs)
34     proc = subprocess.Popen(args, stdin=subprocess.PIPE)
35     proc.communicate(text)
36 cmd_funcs = (lambda: True, sendmail_cmd)
37
38 setup_sendmail, sendmail = smtp_funcs
39 setup_sendmail, sendmail = cmd_funcs
40
41 def parse_arguments():
42     parser = OptionParser(usage='usage: %prog [options] cc_addr template recipients')
43     parser.add_option('-q', '--rt-queue', dest='rt_queue',
44             help='Automatically create a ticket in queue QUEUE',
45             metavar='QUEUE',
46     )
47     parser.add_option('-o', '--rt-owner', dest='rt_owner',
48             help='Set RT owner and AdminCC to USER',
49             metavar='USER',
50     )
51     parser.add_option('--split', dest='split',
52             help='Split "email" field on SPLIT and send to each recipient',
53             metavar='SPLIT',
54     )
55     (options, args) = parser.parse_args()
56     if len(args) != 3:
57         parser.error("incorrect number of arguments")
58     if options.rt_owner and not options.rt_queue:
59         parser.error("--rt-owner requires specifying a queue")
60     return options, args
61
62 def nop_msg_filter(rcpts, body):
63     return rcpts, body
64
65 def msg_filter_factory(opts):
66     if not opts.rt_queue:
67         return nop_msg_filter
68
69     try:
70         import rtkit.tracker, rtkit.authenticators, rtkit.errors
71     except ImportError:
72         print "Note: using rtkit from %s" % (rtkit_path, )
73         sys.path.append(rtkit_path)
74         import rtkit.tracker, rtkit.authenticators, rtkit.errors
75
76     cookie = rtkit.authenticators.CookieAuthenticator
77     resource = rtkit.resource.RTResource.from_rtrc(cookie)
78     parser = email.parser.Parser()
79
80     def filter_rt(rcpts, body, ):
81         msg = parser.parsestr(body)
82         content = {
83             'content': {
84                 'Requestors': ", ".join(rcpts),
85                 'Queue': opts.rt_queue,
86                 'Subject' : msg['Subject'],
87                 'Text' : '',
88             }
89         }
90         if opts.rt_owner:
91             content['content']['AdminCC'] = opts.rt_owner
92             content['content']['Owner'] = opts.rt_owner
93
94         try:
95             response = resource.post(path='ticket/new', payload=content,)
96             results = dict(response.parsed[0])
97             ticket, ticket_number = results['id'].split('/')
98             assert ticket == 'ticket', 'unexpected value "%s" instead of ticket' % (ticket, )
99             subject = "%s [help.mit.edu #%s]" % (msg['Subject'], ticket_number)
100             del msg['Subject']
101             msg['Subject'] = subject
102         except rtkit.errors.RTResourceError as e:
103             logger.error(e.response.status_int)
104             logger.error(e.response.status)
105             logger.error(e.response.parsed)
106
107         # We don't want to send mail to the real recipient, because RT
108         # will send them a copy too.
109         return [], msg.as_string()
110
111     return filter_rt
112
113 def mail_merge(opts, cc_addr, email_file, recipients_file):
114     email_tmpl = open(email_file, 'r').read()
115     reader = csv.reader(open(recipients_file, 'r'))
116     header = reader.next()
117     msg_filter = msg_filter_factory(opts)
118     print header
119     for line in reader:
120         dct = dictize_line(header, line, )
121         print dct
122         text = email_tmpl % dct
123         if opts.split:
124             prop_rcpts = dct['email'].split(opts.split)
125         else:
126             prop_rcpts = [dct['email']]
127         rcpts, text = msg_filter(prop_rcpts, text, )
128         rcpts.append(cc_addr)
129         sendmail(rcpts, text, )
130
131 if __name__=='__main__':
132     options, args = parse_arguments()
133     setup_sendmail()
134     mail_merge(options, *args)