How to fetch an email body using imaplib in python?

后端 未结 4 1364
梦毁少年i
梦毁少年i 2020-11-27 14:01

I\'d like to fetch the whole message from IMAP4 server. In python docs if found this bit of code that works:

>>> t, data = M.fetch(\'1\', \'(RFC822)         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 14:44

    You may use imap_tools package: https://pypi.org/project/imap-tools/

    Features:

    • Parsed email message attributes
    • Query builder for searching emails
    • Work with emails in folders (copy, delete, flag, move, seen)
    • Work with mailbox folders (list, set, get, create, exists, rename, delete, status)
    • No dependencies

    example:

    from imap_tools import MailBox
    
    # get list of email bodies from INBOX folder
    with MailBox('imap.mail.com').login('test@mail.com', 'password', 'INBOX') as mailbox:
        bodies = [msg.text or msg.html for msg in mailbox.fetch()]
    

提交回复
热议问题