Extract sender's email address from Outlook Exchange in Python using win32
Asked Answered
E

3

11

I am trying to extract the sender's email address from outlook 2013 using win32 package in python. There are two kinds of email address type in my Inbox, exchange and smtp. If I try to print the the sender's email address of Exchange type, I am getting this:

/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP(FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=6F467C825619482293F429C0BDE6F1DB-

I have already gone through this link but couldn't find a function through which I can extract the smtp address.

Below is my code:

from win32com.client import Dispatch
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
folders = inbox.Folders
for msg in all_inbox:
   print msg.SenderEmailAddress  

Currently all the Email Address are coming like this:

/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP(FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=6F467C825619482293F429C0BDE6F1DB-

I found a solution to this in VB.net link but don't know how to rewrite the same thing in Python.

Escapade answered 24/7, 2015 at 20:13 Comment(0)
Z
20

Firstly, your code will fail if you have an item other than MailItem in the folder, such as ReportItem, MeetingItem, etc. You need to check that the Class property (exposed by all Outlook objects) is 43 (olMail).

Secondly, you need to check the sender email address type and use the SenderEmailAddress property only for the "SMTP" address type. In VB:

 for each msg in all_inbox
   if msg.Class = 43 Then
     if msg.SenderEmailType = "EX" Then
       print msg.Sender.GetExchangeUser().PrimarySmtpAddress
     Else
       print msg.SenderEmailAddress 
     End If  
   End If
 next
Zendavesta answered 24/7, 2015 at 20:25 Comment(3)
Thanks brother :) you saved so much of my time.Escapade
@python, Is there anyway to extract the email signature alone from the body of the email??Klystron
@Klystron - no, signature is just a part of the HTML body, especially on incoming messages. There is nothing special about it.Zendavesta
E
12

I am just modifying the program given above in Python.

from win32com.client import Dispatch
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
folders = inbox.Folders

for msg in all_inbox:
       if msg.Class==43:
           if msg.SenderEmailType=='EX':
               print msg.Sender.GetExchangeUser().PrimarySmtpAddress
           else:
               print msg.SenderEmailAddress

This will print out all the sender's email address in your inbox folders only.

Escapade answered 24/7, 2015 at 21:24 Comment(2)
In my case, this failed with AttributeError: 'NoneType' object has no attribute 'PrimarySmtpAddress'. How can there be no PrimarySmtpAddres?Piscary
Most probably, your folder is not containing any emails at the time of testing.Impacted
K
2

I had this same problem workin with win32com today. I found the solution here.

Using your example it would be:

from win32com.client import Dispatch
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
folders = inbox.Folders

for msg in all_inbox:
   if msg.Class==43:
       if msg.SenderEmailType=='EX':
           if msg.Sender.GetExchangeUser() != None:
               print msg.Sender.GetExchangeUser().PrimarySmtpAddress
           else:
               print msg.Sender.GetExchangeDistributionList().PrimarySmtpAddress
       else:
           print msg.SenderEmailAddress

This should solve the group mail issue.

Kroon answered 6/1, 2021 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.