Harish Anjaneya
Harish Anjaneya

Reputation: 152

How to read internet headers of email in python?

I want to read internet headers of a mail I received and analyse it on online portals to check if the mail is malicious. I have gone through many web sites and figured win32com will help me with this. Sadly, though i can extract a lot of things, I'm not being able to extract the internet headers. This is what I have done till now:

 import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox=outlook.GetDefaultFolder(6)
messages = inbox.Items
message = messages.GetLast()
mess=message.Body            #Body attribute fetches the body of the message
print mess                   #There is no InternetHeader attribute

The message variable has no Headers or InternetIeaders attribute. Link : https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.mailitem_properties.aspx

Please help me to retrieve my message headers. Thank you in advance!

Upvotes: 2

Views: 1581

Answers (1)

CLeo-Dev
CLeo-Dev

Reputation: 216

It seems you need to read the PR_TRANSPORT_MESSAGE_HEADERS MAPI property using MailItem.PropetyAccessor.GetProperty(String) as said in the following link:

https://msdn.microsoft.com/VBA/Outlook-VBA/articles/propertyaccessor-getproperty-method-outlook

PR_TRANSPORT_MESSAGE_HEADERS DASL property name is "http://schemas.microsoft.com/mapi/proptag/0x007D001F". It is not a link, but a string to use as the function parameter.

Your code will look like:

 import win32com.client
 outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
 inbox=outlook.GetDefaultFolder(6)
 messages = inbox.Items
 message = messages.GetLast()
 mess=message.Body
 internet_header = message.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001F")
 print(internet_header)

I hope it's what you're looking for.

Upvotes: 3

Related Questions