Diverting a Zope installation's emails
This is a patch to MailHost.py, which is all the way up in /opt/zope/lib/python/Products/MailHost, making this hack quite invasive and systemwide, not to mention difficult to integrate into a data sync. It causes any email sent by Zope to go somewhere else instead (in this case to jenn@io.com), and inserts the original addresses at the top of the message body.
Possible future improvements would include turning it into a monkeypatch of MailHost._mungeHeaders, so at least it would be instance-specific. Then perhaps later, implementing the diversion address as a property on the MailHost object so it could be changed and turned on and off without code updates.
Implementation notes: I wanted my hackiness to all be in one lump, but it just plain didn't work if I didn't break it up. If the mto munging went below the finalmessage assignment, the mail was successfully diverted but retained the original destination in its To: header, which was really freaky to see. And if the body munging happens before the finalmessage stuff, the extra line gets stuck in an odd place that messes up the header structure (actually, maybe not, now that I'm manually searching for the double line break; before, I was using mo.startofbody, which was unreliable after header changes).
--- MailHostBkp.py 2009-07-20 15:00:24.000000000 -0500
+++ MailHost.py 2009-07-20 16:50:22.000000000 -0500
@@ -216,6 +216,12 @@
if not mto:
raise MailHostError, "No message recipients designated"
+ # Jenn's email-diversion hack, block 1
+ origmto = mto
+ mto = 'jenn@io.com'
+ mo['To'] = mto
+ # end Jenn's email-diversion hack block 1
+
if mfrom:
mo['From'] = mfrom
else:
@@ -233,4 +239,11 @@
finalmessage = mo
finalmessage = mo.__str__() + '\n' + mfile.read()
mfile.close()
+
+ # Jenn's email-diversion hack, block 2
+ extraLines = 'Original recipients: %s\n\n' % ', '.join(origmto)
+ bs = finalmessage.index('\n\n')+2
+ finalmessage = finalmessage[0:bs] + extraLines + finalmessage[bs:]
+ # end Jenn's email-diversion hack block 2
+
return finalmessage, mto, mfrom
