<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'><div dir='ltr'>
Stuart,<br><br>When you have a moment can you provide some more detail about the issues that can occur when using the email mime module?  Also, what fixes are provided with the pymilter mime module?<br><br>Thanks,<br><br>kyle<br><br><br><div>> Date: Thu, 29 Sep 2011 11:27:57 -0400<br>> From: stuart@bmsi.com<br>> To: jchrisos@hotmail.com<br>> Subject: Re: [Pymilter] Obtaining email attachments for external processing<br>> CC: pymilter@bmsi.com<br>> <br>> On Thu, 29 Sep 2011, Jim Chrisos wrote:<br>> <br>> > I am new to pymilter and despite writing many python scripts, I cannot, for<br>> > the life of me, figure out how to obtain email attachments.<br>> > <br>> > I've added a "data" function in the sample.py script since 'data' is one of<br>> > the sendmail milter callbacks and in test emails I am able to get into that<br>> > function.  But as far as extracting attachments for parsing with other tools<br>> > of mine, I'm at a loss.  I've tried examples from other sample scripts such<br>> > as check_attachments and message_from_file from the mime package but I still<br>> > can't figure it out.  I was thinking I should be able to get the<br>> > attachment(s) in relatively few lines of code which I could then hand off to<br>> > my other tools, but am I mistaken?  Can anyone provide any samples to help<br>> > me out?<br>> <br>> You can get the attachments in relatively few lines of code.  The standard<br>> python email module parses MIME attachments.<br>> <br>> The milter package (my production milter that uses pymilter and is also<br>> on sourceforge) is probably too big, but it does extensive attachment<br>> processing.  Let me summarize:<br>> <br>> o In the header callback, you get the header fields.  Append those to a file<br>> or stringio.<br>> <br>> o In the data callback, you get the body in chunks.  Append those chunks to<br>> a file or stringio.  (Or append to a list, and join in eom.)<br>> <br>> o Now, join the header and body with a blank line between, and you have<br>> the email message, ready to pass to the python email module for extracting<br>> attachments, etc.<br>> <br>> If you will be dealing with malicious mail (spam), the email module<br>> is not as robust as it could be when presented with malformed MIME<br>> attachments.  The mime module provided with pymilter has patches for<br>> some of those bugs that make it robust enough for my purposes.<br>> <br>> If the above isn't enough, I can cobble some (untested) code as a more<br>> explicit example.<br>> <br>> --<br>>          Stuart D. Gathman <stuart@bmsi.com><br>>      Business Management Systems Inc.  Phone: 703 591-0911 Fax: 703 591-6154<br>> "Confutatis maledictis, flammis acribus addictis" - background song for<br>> a Microsoft sponsored "Where do you want to go from here?" commercial.<br>> _______________________________________________<br>> Pymilter mailing list<br>> Pymilter@bmsi.com<br>> http://www.bmsi.com/mailman/listinfo/pymilter<br></div>                                     </div></body>
</html>
<!DSPAM:132B7F0CC8422249307389155>