How do I identify Outlook "message id" over imap/pop3 in Python?

2k Views Asked by At

Problem

I am using a "logic app" in Azure to create a queue of incoming mails. The way the emails are registered are using a "message id", which is described as "a unique identifier for a message". I would like to be able to fetch emails over imap using this id - is this possible?

Logic app "message id"

  1. Example of "message id":

AQMkADAwATM3ZmYAZS0yNTYwLWNkZAAzLTAwAi0wMAoARgAAA-U4TGbG56lEtdoXy_23gW0HAKhWKDtf5AJErHyhh_b9NYQAAAIBDAAAAKhWKDtf5AJErHyhh_b9NYQAAAIFfgAAAA==

  1. Example of logic app:

enter image description here

What I have tried

I have tried just to download all emails as eml, and then to read them into notepad++ to see if the "message id" even exists in the eml-files, but they don't.

# Library for downloading emails
import imaplib

# Logging in
mail = imaplib.IMAP4_SSL("outlook.office365.com",993)
mail.login(email_user, email_pass)

# Downloading emails to eml
mail.select('Inbox')
typ, data = mail.search(None, 'ALL')
for num in data[0].split():
    typ, data = mail.fetch(num, '(RFC822)')
    f = open('%s/%s.eml' %("/my/path/", num), 'wb')
    f.write(data[0][1])
mail.close()
mail.logout()
1

There are 1 best solutions below

2
Mohit Verma On

May i know why are you trying to fetch email over IMAP . As you can fetch fetch email using message id from outlook api too. Here is the api which you can use:

GET https://outlook.office.com/api/v2.0/me/messages/{message_id}

You can find more details here:

https://learn.microsoft.com/en-us/previous-versions/office/office-365-api/api/version-2.0/mail-rest-operations#GetMessages

Also just to update , in outlook you wont' find the message id in .eml or in body, it's available in internet header. Most clients utilities download the headers (including the Message-ID) of all messages, store them, and then post-process them. For outlook you can find it in internet header like below:

enter image description here

enter image description here

Reference: https://www.codetwo.com/kb/messageid/

Still if you want to access email using IMAP, try below thread and see if it helps:

https://www.go4expert.com/articles/accessing-email-using-imap-python-t28838/

https://social.msdn.microsoft.com/Forums/en-US/29f44441-feda-4f81-a04c-40d53b3dfdc5/how-to-access-an-email-using-messageid-in-outlook?forum=outlookdev

Hope it helps.