Im trying to use win32com.client to print the body message of an outlook email but cannot figure it out. If anyone could help thatd be amazing

Viewed 628
import win32com.client
import os
Outlook = win32com.client.Dispatch("Outlook.Application")
olNs = Outlook.GetNamespace("MAPI")
Inbox = olNs.GetDefaultFolder(6)

Filter = "[Subject] = 'John Doe Test Results'"

Items = Inbox.Items.Restrict(Filter)
Item = Items.GetFirst()
messages = Inbox.Items
print(type(messages))
for attachment in Item.Attachments:
    print(attachment.FileName)
    attachment.SaveAsFile(os.path.join(r"C:\Users\Conrad\Desktop\test\New folder" + attachment.FileName))

this downloads the attachment on the email and puts it in a folder on my desktop. I want to be able to print the body of the email so further on I can add it to a dataframe and send it to excel, but I just need help pulling and printing the body message.

1 Answers

You can loop through the messages and print the body of each message:

import win32com.client
import os
Outlook = win32com.client.Dispatch("Outlook.Application")
olNs = Outlook.GetNamespace("MAPI")
Inbox = olNs.GetDefaultFolder(6)

Filter = "[Subject] = 'John Doe Test Results'"

Items = Inbox.Items.Restrict(Filter)
Item = Items.GetFirst()
messages = Inbox.Items
#NEW CODE
for message in messages:
    print(message.body)

print(type(messages))
for attachment in Item.Attachments:
    print(attachment.FileName)
    attachment.SaveAsFile(os.path.join(r"C:\Users\Conrad\Desktop\test\New folder" + attachment.FileName))
Related