[Automation] Send Outlook email with Python

Recently, chat-based communication tools such as LINE have become mainstream.

However, according to the "Business Email Survey 2020" of the Japan Business Email Association, ** "email" (99.1%) ** is the number one main means of communication used at work.

Email is a hassle ...

Since my company is "email culture", I exchange all kinds of information by email.

The most troublesome thing is [Email software bug status to team members every evening] That is.

Isn't there a lot of clerical work in every company that ** emails similar information to similar members on a regular basis **?

This kind of mechanical work is the most effective for automation.

Let's see the concrete method immediately.

Send Outlook email in Python

According to the "Business Email Survey 2020" mentioned earlier, "Outlook" (50.52%) and "Gmail" (38.40%) are the two strongest email software used at work.

Therefore, in this article, I will introduce how to compose an email in Outlook.

You need to import win32com.client to operate outlook. I'm using Anaconda and I was able to import it without any additional installation.

python


import win32com.client

Next, create an Outlook object.

python


outlook = win32com.client.Dispatch("Outlook.Application")

And then create a mail object.

python


mail = outlook.CreateItem(0)

By changing the n part of CreateItem (n), you can create various items such as Outlook mail and calendar. Set to 0 to create an email object.

By setting attributes to this mail object, we will create a mail.

attribute meaning
mail.to destination
mail.cc CC
mail.bcc BCC
mail.subject subject
mail.bodyFormat Email format
1: Text
2:HTML
3: Rich text
mail.body Text

python



mail.to = '[email protected]; [email protected]'
mail.cc = '[email protected]'
mail.bcc = '[email protected]'
mail.subject = 'Level 1 exam'
mail.bodyFormat = 1
mail.body = '''thank you for your hard work. This is Doremi.

It seems that this year's exam was a remote interview due to the influence of Corona.
I just heard from Majolica.

I was surprised, so I will contact you as soon as possible.

Thank you.
'''

We recommend that you check the emails you have created so far before sending them. To check, use mail.display (True).

python



mail.display(True)

The email will open like this, so check it and send it if it's OK.

1級試験の件.png

We hope that you will be freed from the hassle of sending emails. Finally, I will put the code together.

python


import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application")

mail = outlook.CreateItem(0)

mail.to = '[email protected]; [email protected]'
mail.cc = '[email protected]'
mail.bcc = '[email protected]'
mail.subject = 'Level 1 exam'
mail.bodyFormat = 1
mail.body = '''thank you for your hard work. This is Doremi.

It seems that this year's exam was a remote interview due to the influence of Corona.
I just heard from Majolica.

I was surprised, so I will contact you as soon as possible.

Thank you.
'''

mail.display(True)

How to read Outlook mail?

This time we introduced you to automating Outlook email sending, but see this article for how to read Outlook emails in Python. https://qiita.com/konitech913/items/fa0cf66aad27d16258c0

Recommended Posts

[Automation] Send Outlook email with Python
[Python] Send an email with outlook
Send email with Python
Send Japanese email with Python3
Send email in Python (Outlook)
[Automation] Extract Outlook appointments with Python
[Python] Send email
[Python] Send email
Send email via gmail with Python 3.4.3.
Send an email with Amazon SES + Python
Send email with Django
Validate E-Mail with Python
Send an email with Excel attached in Python
Send using Python with Gmail
[Python] Send an email from gmail with two-step verification set
[Automation with python! ] Part 1: Setting file
Easy email sending with haste python3
[Automation with python! ] Part 2: File operation
Note: Send an email with Django
Send multipart / form-data with python requests
Send image with python, save with php
[Automation] Manipulate mouse and keyboard with Python
Send email to multiple recipients in Python (Python 3)
I tried sending an email with python.
[Automation] Read a Word document with Python
[Automation] Read mail (msg file) with Python
Python 3.6 email library
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Send an email to a specific email address with python without SMTP settings
Send email with SES in Python and short message with SMS on SNS
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
[Outlook] I tried to automatically create a daily report email with Python
Excel with Python
Microcomputer with Python
Cast with python
Send a message to LINE with Python (LINE Notify)
[Automation] Extract the table in PDF with Python
GUI automation with Python x Windows App Driver
Sample to send slack notification with python lambda
Extract email attachments received in Thunderbird with Python
I tried sending an email with SendGrid + Python
Send HTTP with Basic authentication header in Python
To automatically send an email with an attachment using the Gmail API in Python
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python