From: Alex Dehnert Date: Wed, 13 Mar 2013 09:26:28 +0000 (-0400) Subject: mail-merge: allow sending to multiple emails X-Git-Url: https://www.dehnerts.com/gitweb/?p=user%2Falex%2Fsoftware%2Fmy-snippets.git;a=commitdiff_plain;h=b3c45f41f515124b954a40e21e7fa2b71d476fcf mail-merge: allow sending to multiple emails This adds an option, --split=SPLIT, that allows splitting the "email" field to send to several recipients. --- diff --git a/mail-merge b/mail-merge index 4e5f353..d7375f8 100755 --- a/mail-merge +++ b/mail-merge @@ -48,6 +48,10 @@ def parse_arguments(): help='Set RT owner and AdminCC to USER', metavar='USER', ) + parser.add_option('--split', dest='split', + help='Split "email" field on SPLIT and send to each recipient', + metavar='SPLIT', + ) (options, args) = parser.parse_args() if len(args) != 3: parser.error("incorrect number of arguments") @@ -55,8 +59,8 @@ def parse_arguments(): parser.error("--rt-owner requires specifying a queue") return options, args -def nop_msg_filter(rcpt, body): - return rcpt, body +def nop_msg_filter(rcpts, body): + return rcpts, body def msg_filter_factory(opts): if not opts.rt_queue: @@ -73,11 +77,11 @@ def msg_filter_factory(opts): resource = rtkit.resource.RTResource.from_rtrc(cookie) parser = email.parser.Parser() - def filter_rt(rcpt, body, ): + def filter_rt(rcpts, body, ): msg = parser.parsestr(body) content = { 'content': { - 'Requestors': rcpt, + 'Requestors': ", ".join(rcpts), 'Queue': opts.rt_queue, 'Subject' : msg['Subject'], 'Text' : '', @@ -102,7 +106,7 @@ def msg_filter_factory(opts): # We don't want to send mail to the real recipient, because RT # will send them a copy too. - return None, msg.as_string() + return [], msg.as_string() return filter_rt @@ -116,10 +120,12 @@ def mail_merge(opts, cc_addr, email_file, recipients_file): dct = dictize_line(header, line, ) print dct text = email_tmpl % dct - rcpt, text = msg_filter(dct['email'], text, ) - rcpts = [cc_addr] - if rcpt: - rcpts.append(rcpt) + if opts.split: + prop_rcpts = dct['email'].split(opts.split) + else: + prop_rcpts = [dct['email']] + rcpts, text = msg_filter(prop_rcpts, text, ) + rcpts.append(cc_addr) sendmail(rcpts, text, ) if __name__=='__main__':