vyi
vyi

Reputation: 1092

Detect and extract attachment from email. using imap in python

I couldn't find information on extracting and detecting email attachment in python imap library documentation. On reading the header of message i saw keywords 'attachment' 'type' etc.. but I WISH TO KNOW IF THERE IS A Straight path to Heaven(Email attachment boundary and data extraction)

Upvotes: 1

Views: 3814

Answers (1)

Vladimir
Vladimir

Reputation: 6726

External lib: https://github.com/ikvk/imap_tools

from imap_tools import MailBox

# get all attachments from INBOX and save them to files
with MailBox('imap.my.ru').login('acc', 'pwd', 'INBOX') as mailbox:
    for msg in mailbox.fetch():
        for att in msg.attachments:
            print(att.filename, att.content_type)
            with open('C:/1/{}'.format(att.filename), 'wb') as f:
                f.write(att.payload)

Upvotes: 1

Related Questions