[Tutor] retreiving email file attachments

Jim Haak HaakJ@masirv.com
Tue, 11 Jun 2002 11:30:06 -0700


Jeff,

Thanks very much.  That works great.  

Jim Haak 

*-----Original Message-----
*From: Jeff Shannon [mailto:jeff@ccvcorp.com]
*Sent: Monday, June 10, 2002 5:44 PM
*To: Jim Haak
*Cc: 'tutor@python.org'
*Subject: Re: [Tutor] retreiving email file attachments
*
*
*
*
*Jim Haak wrote:
*
*> Does anyone have an example of using poplib to read an Inbox and then
*> passing the messages to the email package?   I am trying to 
*parse zipped
*> attachments.  [...]
*>
*> Since the email module wants a file, I've tried writing all 
*the 'lines' to a
*> file, but that doesn't seem to work.
*
*I haven't done this myself, but here's some ideas for you to research.
*
*The POP3.retr() method returns a tuple, the second member of 
*which is (iirc) a
*list of lines.  You can probably safely discard the other two items.
*
*lines = pop.retr(n)[1]
*
*You need to feed a file to the email module, but saving all of 
*this to a file
*would be pointless.  The solution is the StringIO (or better 
*yet, cStringIO),
*which will convert a string into a file-like object.
*
*import cStringIO
*fakefile = cStringIO.StringIO( '\n'.join(lines) )
*
*I use '\n'.join() to convert the list of lines into a single 
*string with newline
*separators, then feed that to StringIO to create a file-like 
*object.  This
*object is (for most purposes) no different than the object returned by
*open(filename).  You should then be able to give this StringIO 
*object to the
*email package.
*
*Hope that this helps...
*
*Jeff Shannon
*Technician/Programmer
*Credit International
*
*
*