mail-merge: only send one copy with RT
authorAlex Dehnert <adehnert@mit.edu>
Mon, 18 Feb 2013 06:00:40 +0000 (01:00 -0500)
committerAlex Dehnert <adehnert@mit.edu>
Mon, 18 Feb 2013 06:00:40 +0000 (01:00 -0500)
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.

mail-merge

index 5a1e35b5ba53e062135fb3818e30b83d0f33787a..10ab6457a287eeb55ebb73e505f5745dd0a12c15 100755 (executable)
@@ -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()