[Pymilter] Obtaining email attachments for external processing

Stuart D. Gathman stuart at bmsi.com
Thu Sep 29 14:20:34 EDT 2011


On Thu, 29 Sep 2011, Stuart D. Gathman wrote:

> If the above isn't enough, I can cobble some (untested) code as a more
> explicit example.

Ok, have a few minutes, here is how to collect the body:

   @Milter.noreply
   def envfrom(self,f,*str):
     self.log("mail from",f,str)
     self.fp = StringIO.StringIO()

   @Milter.noreply
   def header(self,name,hval):
     if self.fp:
       self.fp.write("%s: %s\n" % (name,hval))    # add decoded header to buffer
     return Milter.CONTINUE

   @Milter.noreply
   def eoh(self):
     if self.fp:
       self.fp.write("\n")                         # terminate headers
     return Milter.CONTINUE

   @Milter.noreply
   def body(self,chunk):         # copy body to temp file
     try:
       if self.fp:
         self.fp.write(chunk)      # IOError causes TEMPFAIL in milter
         self.bodysize += len(chunk)
     except Exception,x:
       if not self.ioerr:
         self.ioerr = x
         self.log(x)
       self.fp = None
     return Milter.CONTINUE

   def eom(self):
     if self.fp:
       self.fp.seek(0)
       msg = mime.message_from_file(self.fp)
       # msg is an email.message.Message
       # http://docs.python.org/release/2.6.6/library/email.message.html
       ...

--
 	      Stuart D. Gathman <stuart at bmsi.com>
     Business Management Systems Inc.  Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flammis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.



More information about the Pymilter mailing list