[Pymilter] Editing only the body of a message

Stuart D Gathman stuart at bmsi.com
Wed Jan 29 20:49:08 EST 2014


On 01/29/2014 01:48 PM, Zack Allen wrote:
> I am using the template file located here 
> http://pythonhosted.org/pymilter/milter-template_8py-example.html to 
> create my own kilter. For now, this milter should be adding a custom 
> footer to every email message sent through. The issue I am having is 
> that self.replacebody(self.fp.getvalue() + footer) destroys HTML 
> messages and messages with attachments. Is there a way to specifically 
> edit *only* the body of the message, and keep the integrity of HTML, 
> attachments and headers?
>
You are processing the message at the rfc2821 level.  There is no such 
thing as HTML or attachments - which are added at the rfc2822 level via 
standards such as MIME or Microsoft TNEF.

Note that the sample (template) uses the email package to split the 
message into attachments in the 'msg' object.  You can iterate through 
those to identify the "body" (not an official term - perhaps the first 
text/plain attachment?)  Please see the email API for replacing a 
particular attachment.  After making your changes, you extract the SMTP 
form of the email body (with all the attachment encoded as MIME) from 
the msg object with msg.dump(outfp).  The milter (not pymilter) project 
uses pymilter and does quite a bit of MIME attachment replacement and 
scanning.  You might get some ideas from its code (e.g. hooking 
msg.headerchange), although it does maybe too many things.

Note that the message collected by the template in self.fp includes the 
email header (with fields like From:, To:, etc).  You can:

a) not collect header data in the header and eoh callbacks.  The 
template does that to enable using the email package.

b) split the header and body with something like hdr,body = 
self.fp.getvalue().split('\n\n',1)
      You then pass just the body to replacebody().  (Or hdr,body = 
msg.dump().split('\n\n',1) if using the email package to parse attachments.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://gathman.org/pipermail/pymilter/attachments/20140129/73c687c8/attachment.html>


More information about the Pymilter mailing list