Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams What part of the Python library have you read so far? Have you looked at the pop or imap libraries at all? S.Lott Nov 1, 2010 at 10:12 I've only look into email.message.Message and mimetools.Message. Ok I will read into pop and imail and see what I can do with it. Joshua Partogi Nov 1, 2010 at 10:18

I don't really understand what you mean by "email multipart message object". Do you mean an object belonging to the email.message.Message class?

If that is what you mean, it's straightforward. On a multipart message, the get_payload method returns a list of message parts (each of which is itself a Message object). You can iterate over these parts and examine their properties: for example, the get_content_type method returns the part's MIME type, and the get_filename method returns the part's filename (if any is specified in the message). Then when you've found the correct message part, you can call get_payload(decode=True) to get the decoded contents.

>>> import email
>>> msg = email.message_from_file(open('message.txt'))
>>> len(msg.get_payload())
>>> attachment = msg.get_payload()[1]
>>> attachment.get_content_type()
'image/png'
>>> open('attachment.png', 'wb').write(attachment.get_payload(decode=True))

If you're programmatically extracting attachments from email messages you have received, you might want to take precautions against viruses and trojans. In particular, you probably ought only to extract attachments whose MIME types you know are safe, and you probably want to pick your own filename, or at least sanitize the output of get_filename.

HTML mails often have images in the footers, which are also sent as attachments. You can distinguish these from "real" attachments by looking at the Content-Disposition: inline images start with "inline", while actual attachments start with"attachment". There's no method for getting the content disposition, but you can call part.get('Content-Disposition').startswith('attachment') if you're only interested in actual attachments. – jrial Jun 25, 2017 at 15:21 typ, data = self.imap.uid('SEARCH', 'ALL') msgs = data[0].split() print "Found {0} msgs".format(len(msgs)) for uid in msgs: typ, s = self.imap.uid('FETCH', uid, '(RFC822)') mail = email.message_from_string(s[0][1]) print "From: {0}, Subject: {1}, Date: {2}\n".format(mail["From"], mail["Subject"], mail["Date"]) if mail.is_multipart(): print 'multipart' for part in mail.walk(): ctype = part.get_content_type() if ctype in ['image/jpeg', 'image/png']: open(part.get_filename(), 'wb').write(part.get_payload(decode=True))

Actually using now-suggested email.EmailMessage API (don't confuse with old email.Message API) it is fairly easy to:

  • Iterate over all message elements and select only attachments

  • Iterate over just attachments

  • Let's assume that you have your message stored as byte content in envelope variable

    Solution no.1:

    import email
    from email.message import EmailMessage
    email_message: EmailMessage = email.message_from_bytes(envelope, _class=EmailMessage)
    for email_message_part in email_message.walk():
        if email_message.is_attachment():
            # Do something with your attachment
    

    Solution no.2: (preferable since you don't have to walk through other parts of your message object)

    import email
    from email.message import EmailMessage
    email_message: EmailMessage = email.message_from_bytes(envelope, _class=EmailMessage)
    for email_message_attachment in email_message.iter_attachments():
            # Do something with your attachment
    

    Couple things to note:

  • We explicitly tell to use new EmailMessage class in our byte read method through _class=EmailMessage parameter
  • You can read your email message (aka envelope) from sources such as bytes-like object, binary file object or string thanks to built-in methods in message.Parser API
  • Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.