From: Alex Dehnert Date: Mon, 18 Feb 2013 06:00:40 +0000 (-0500) Subject: mail-merge: only send one copy with RT X-Git-Url: https://www.dehnerts.com/gitweb/?p=user%2Falex%2Fsoftware%2Fmy-snippets.git;a=commitdiff_plain;h=4acdc3517b9150ffa010bf0f30c473fbd473fae7 mail-merge: only send one copy with RT Previously, the RT mode for mail-merge would send one copy directly to the recipient, and one to the user through RT. This changes mail-merge to only send through RT, so the user only gets one copy. --- diff --git a/mail-merge b/mail-merge index 5a1e35b..10ab645 100755 --- a/mail-merge +++ b/mail-merge @@ -54,9 +54,12 @@ def parse_arguments(): parser.error("--rt-owner requires specifying a queue") return options, args +def nop_msg_filter(rcpt, body): + return rcpt, body + def msg_filter_factory(opts): if not opts.rt_queue: - return lambda rcpt, body: body + return nop_msg_filter import rtkit.tracker, rtkit.authenticators, rtkit.errors cookie = rtkit.authenticators.CookieAuthenticator @@ -90,12 +93,14 @@ def msg_filter_factory(opts): logger.error(e.response.status) logger.error(e.response.parsed) - return msg.as_string() + # 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 filter_rt def mail_merge(opts, cc_addr, email_file, recipients_file): - email = open(email_file, 'r').read() + email_tmpl = open(email_file, 'r').read() reader = csv.reader(open(recipients_file, 'r')) header = reader.next() msg_filter = msg_filter_factory(opts) @@ -103,10 +108,12 @@ def mail_merge(opts, cc_addr, email_file, recipients_file): for line in reader: dct = dictize_line(header, line, ) print dct - text = email % dct - text = msg_filter(dct['email'], text, ) - print text - sendmail([dct['email'], cc_addr, ], text, ) + text = email_tmpl % dct + rcpt, text = msg_filter(dct['email'], text, ) + rcpts = [cc_addr] + if rcpt: + rcpts.append(rcpt) + sendmail(rcpts, text, ) if __name__=='__main__': options, args = parse_arguments()