Download an attachment from an IMAP email

Kushal Kumaran kushal.kumaran+python at
Fri Feb 4 02:52:02 CET 2011

On Fri, Feb 4, 2011 at 3:44 AM, Vincent Davis <vincent at> wrote:
> I have a few emails I am trying to download from my google account. I seem
> to be getting the message but each of these messages have an attachment. I
> don't understand what I ned to do to get and save the attachment to a local
> file.
> Here is what I have so far.
> rc, resp = M.login('xxxxx at xxxx', 'XXXXX')
> print rc, resp
>'[Gmail]/All Mail')
>, 'FROM', 'someone at')
> #M.fetch(121, '(body[header.fields (subject)])')
> M.fetch(121, '(RFC822)')

Take a look at the email module.  The message_from_string() function
can convert the string representation of the email (as obtained by
M.fetch(121, '(RFC822)') into a message object.


More information about the Python-list mailing list