nJoshi
nJoshi

Reputation: 376

Read outlook 365 email using python and get sender email (exchange or SMTP)

I have a python script to read emails from the inbox folder in outlook, and retrive the email id of the sender.


    outlook = win32com.client.Dispatch(
        "Outlook.Application").GetNamespace("MAPI")
    print("Reading Mails")
    inbox = outlook.GetDefaultFolder(6)
    messages = inbox.Items
    for msg in messages:
        print(msg.SenderEmailAddress)

prints '/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBONPDLT)/cn=Recipients/cn=80cf94566sdfhve819ddaede72dc842-Sender Name'

instead of a email ID that the script can work on.

Upvotes: 1

Views: 3419

Answers (1)

nJoshi
nJoshi

Reputation: 376

The printed statement is actually a Exchange address. This returned when the email is received from within a company. The best way to handle this is to identify if the SenderEmailType is Exchange.

    if (msg.SenderEmailType = "EX"):
        print(msg.Sender.GetExchangeUser().PrimarySmtpAddress)
    else #email type SMTP
        print(msg.SenderEmailAddress)        

Upvotes: 1

Related Questions