[Automation] Read mail (msg file) with Python

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.

Read msg file in Python

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.

Send Outlook email

See this article for how to send Outlook emails in Python. https://qiita.com/konitech913/items/51867dbe24a2a4272bb6

Recommended Posts

[Automation] Read mail (msg file) with Python
[Automation with python! ] Part 1: Setting file
[Automation with python! ] Part 2: File operation
Read CSV file with python (Download & parse CSV file)
Let's read the RINEX file with Python ①
[Automation] Read a Word document with Python
Read Python csv file
How to read a CSV file with Python 2/3
[Python] How to read excel file with pandas
Read table data in PDF file with Python
Read line by line from a file with Python
Read csv with python pandas
Draw netCDF file with python
Read QR code from image file with Python (Mac)
Read json file with Python, format it, and output json
Download csv file with python
Read json data with python
[Python] Read the csv file and display the figure with matplotlib
How to read an Excel file (.xlsx) with Pandas [Python]
Extract the xz file with python
[Automation] Extract Outlook appointments with Python
[Python] Write to csv file with Python
Implemented file download with Python + Bottle
Output to csv file with Python
[Automation] Send Outlook email with Python
Create an Excel file with Python3
[python] Read information with Redmine API
Read files in parallel with Python
Read fbx from python with cinema4d
Read a file in Python with a relative path from the program
[Python] Read a csv file with a large data size using a generator
Read file
Creating a simple PowerPoint file with Python
Exclusive control with lock file in Python
[Automation] Manipulate mouse and keyboard with Python
[C] [python] Read with AquesTalk on Linux
Read the file line by line in Python
Create Excel file with Python + similarity matrix
[Python] Read images with OpenCV (for beginners)
Record with Python → Save file (sounddevice + wave)
Read a character data file with numpy
[python] Read html file and practice scraping
Read text in images with python OCR
I made a configuration file with Python
Read and format a csv file mixed with comma tabs with Python pandas
Read a Python # .txt file for a super beginner in Python with a working .py
Read the file with python and delete the line breaks [Notes on reading the file]
Read CSV file with Python and convert it to DataFrame as it is
Split mol2 file with python (-> 2016.04.17 Also supports sdf file)
nginxparser: Try parsing nginx config file with Python
Script python file
Statistics with python
Read data with python / netCDF> nc.variables [] / Check data size
Read a file containing garbled lines in Python
Read Python csv data with Pandas ⇒ Graph with Matplotlib
Python with Go
Read JSON with Python and output as CSV
[Automation] Extract the table in PDF with Python
Convert svg file to png / ico with Python
Twilio with Python
Medical image analysis with Python 1 (Read MRI image with SimpleITK)