From zma4580 at gmail.com Wed Jan 29 13:48:55 2014 From: zma4580 at gmail.com (Zack Allen) Date: Wed, 29 Jan 2014 13:48:55 -0500 Subject: [Pymilter] Editing only the body of a message Message-ID: Hi All, 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? Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From stuart at bmsi.com Wed Jan 29 20:49:08 2014 From: stuart at bmsi.com (Stuart D Gathman) Date: Wed, 29 Jan 2014 20:49:08 -0500 Subject: [Pymilter] Editing only the body of a message In-Reply-To: References: Message-ID: <52E9AF94.9020007@bmsi.com> 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: