Read Outlook emails in Python

I want to automate analog work

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.

Read Outlook emails in Python

Premise

As a premise, my Outlook mail folder looks like this.

outlook_folders.png

Broadly speaking

  1. Personal folder
  2. Yahoo mail folder There is, and it is further divided into several folders.

This time, suppose you want to read the mail ** in the "Inbox" of "2. Yahoo Mail Account Folder".

Load 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.

Read mail

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?

Summary

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

Read Outlook emails in Python
Read DXF in python
Read Euler's formula in Python
Send email in Python (Outlook)
Read Namespace-specified XML in Python
Read Fortran output in python
Read PNG chunks in Python (class)
Read files in parallel with Python
Create and read messagepacks in Python
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Read the file line by line in Python
Read the file line by line in Python
Plink in Python
Constant in python
Read and write JSON files in Python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
[Python] Read the specified line in the file
Hashable in python
DirectLiNGAM in Python
Read text in images with python OCR
LiNGAM in Python
Flatten in python
[python] Read data
flatten in python
Read a file containing garbled lines in Python
Read table data in PDF file with Python
Daily AtCoder # 36 in Python
Clustering text in Python
Implement Enigma in python