In a previous article, I showed you how to read folder and email information from Outlook.
Read Outlook emails in Python https://qiita.com/konitech913/items/8a285522b0c118d5f905
Even if you don't do this, there may be cases where you want to ** read a single email data (msg file) and read the information in the email **.
For example, you may want to save the "inquiry mail" as a msg file in a certain folder on your PC, extract the information from that file, and copy it to Excel.
You need to import win32com.client to operate outlook. I'm using Anaconda and I was able to import it without any additional installation.
import win32com.client
Next, create an Outlook object.
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
Then, use the method ʻOpenSharedItem ("xxx.msg") `to read the target msg file. Here, read the file "This is a test.msg".
mail = outlook.OpenSharedItem("This is a test.msg")
This mail is the instance that represents the mail. The meanings of the attributes are as shown in the table below.
attribute | meaning |
---|---|
mail.subject | subject |
mail.sendername | From name |
mail.senderEmailAddress | Sender's email address |
mail.receivedtime | Received date and time |
mail.body | Text |
mail.Unread | Unread flag |
print("subject: " ,mail.subject)
print("From: %s [%s]" % (mail.sendername, mail.senderEmailAddress))
print("Received date and time: ", mail.receivedtime)
print("Unread: ", mail.Unread)
print("Text: ", mail.body)
Execution result
subject:This is a test
From: ***[*********@gmail.com]
Received date and time: 2020-05-30 07:17:33+00:00
Unread: False
Text:Do you receive it properly?
You can read it properly.
See this article for how to send Outlook emails in Python. https://qiita.com/konitech913/items/51867dbe24a2a4272bb6
Recommended Posts