Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

imap-tools Access Raw Message Data

How do you access the raw message data of an email when using imap-tools?
Specifically so it can then be loaded into the email.message_from_bytes() function for forwarding?

from imap_tools import MailBox, AND
with MailBox('imap.gmail.com').login('asdf@gmail.com', '123456', 'INBOX') as mailbox:
    # get unseen emails from INBOX folder
    for msg in mailbox.fetch(AND(seen=False), mark_seen=False):
        pass # get the raw data from msg

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The raw message data for an email fetched with imap-tools can be accessed via the msg.obj property. To parse this into an email.message object, we can pass the raw data to email.message_from_bytes().
So the key is using msg.obj to get the raw message from imap-tools, and then load that into the email library.

from imap_tools import MailBox, AND
import email

with MailBox('imap.gmail.com').login('asdf@gmail.com', '123456', 'INBOX') as mailbox:
   for msg in mailbox.fetch(AND(seen=False), mark_seen=False):
       raw_email = msg.obj
       email_message = email.message_from_bytes(raw_email)
       
       # forward or process email_message here
       

I took the obj object because according to the official documentation it is " the email.message.Message: original object" I pasted you all valid field down below so you can chose if obj is the right fo ryou

for msg in mailbox.fetch():  # generator: imap_tools.MailMessage
    msg.uid          # str | None: '123'
    msg.subject      # str: 'some subject 你 привет'
    msg.from_        # str: 'Bartölke@ya.ru'
    msg.to           # tuple: ('iam@goo.ru', 'friend@ya.ru', )
    msg.cc           # tuple: ('cc@mail.ru', )
    msg.bcc          # tuple: ('bcc@mail.ru', )
    msg.reply_to     # tuple: ('reply_to@mail.ru', )
    msg.date         # datetime.datetime: 1900-1-1 for unparsed, may be naive or with tzinfo
    msg.date_str     # str: original date - 'Tue, 03 Jan 2017 22:26:59 +0500'
    msg.text         # str: 'Hello 你 Привет'
    msg.html         # str: '<b>Hello 你 Привет</b>'
    msg.flags        # tuple: ('\\Seen', '\\Flagged', 'ENCRYPTED')
    msg.headers      # dict: {'received': ('from 1.m.ru', 'from 2.m.ru'), 'anti-virus': ('Clean',)}
    msg.size_rfc822  # int: 20664 bytes - size info from server (*useful with headers_only arg)
    msg.size         # int: 20377 bytes - size of received message

    for att in msg.attachments:  # list: imap_tools.MailAttachment
        att.filename             # str: 'cat.jpg'
        att.payload              # bytes: b'\xff\xd8\xff\xe0\'
        att.content_id           # str: 'part45.06020801.00060008@mail.ru'
        att.content_type         # str: 'image/jpeg'
        att.content_disposition  # str: 'inline'
        att.part                 # email.message.Message: original object
        att.size                 # int: 17361 bytes

    msg.obj              # email.message.Message: original object
    msg.from_values      # imap_tools.EmailAddress | None
    msg.to_values        # tuple: (imap_tools.EmailAddress,)
    msg.cc_values        # tuple: (imap_tools.EmailAddress,)
    msg.bcc_values       # tuple: (imap_tools.EmailAddress,)
    msg.reply_to_values  # tuple: (imap_tools.EmailAddress,)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading