In my daily work, the "inquiry number", "inquiry title", "URL", "receipt date", etc. in the email received by Outlook are copied to the management table (Excel) stored in the server, which is a super analog. There is work.
I wondered if I could somehow automate this task, so I'll start by ** reading the emails I received in Outlook with Python **.
I did a lot of research, but I didn't have much information and I managed to do it through trial and error, so I will leave the information as a memorandum. (The Office in my house is very old and Outlook 2007 ... I think it's probably the same with the latest Office, but please forgive me if it doesn't work.)
The final code is very simple.
As a premise, my Outlook mail folder looks like this.
Broadly speaking
This time, suppose you want to read the mail ** in the "Inbox" of "2. Yahoo Mail Account Folder".
You need to import win32com.client to work with 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")
This object called outlook has an attribute called Folders, which corresponds to "1. Personal folder" and "2. Yahoo mail account folder".
accounts = outlook.Folders
for account in accounts:
print(account)
result
Personal folder
**********@yahoo.co.jp
This account also has an attribute called Folders, which corresponds to folders in the lower hierarchy.
To summarize so far
print("root (Number of accounts=%d)" % accounts.Count)
for account in accounts:
print("└ ",account)
folders = account.Folders
for folder in folders:
print(" └ ",folder)
Execution result
root (Number of accounts=2)
└ Personal folder
└ Deleted items
└ Inbox
└ Outbox
└ Sent items
└ Calendar
└ Contact information
└ History
└ Memo
└ Work
└ Draft
└ RSS feed
└ Spam
└ **********@yahoo.co.jp
└ Inbox
└ Bulk Mail
└ Draft
└ Sent
└ Trash
Besides, it's simple.
The actual mail is contained in the finally obtained folder, but this folder has an attribute called Items, which corresponds to "mail".
Since the mails obtained by Items are iterable objects, they are fetched one by one with the for statement.
mails = folder.Items
for mail in mails:
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)
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 |
Then you can see that the contents of the email can be obtained like this.
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?
Finally, I will summarize the code.
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
accounts = outlook.Folders
print("root (Number of accounts=%d)" % accounts.Count)
for account in accounts:
print("└ ",account)
folders = account.Folders
for folder in folders:
print(" └ ",folder)
mails = folder.Items
for mail in mails:
print("-----------------")
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
root (Number of accounts=2)
└ Personal folder
└ Deleted items
└ Inbox
└ Outbox
└ Sent items
└ Calendar
└ Contact information
└ History
└ Memo
└ Work
└ Draft
└ RSS feed
└ Spam
└ **********@yahoo.co.jp
└ Inbox
-----------------
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?
Recommended Posts