Send email in Python (Outlook)

Introduction

I would like to follow the program that sends emails in Python, starting from the beginning.

The environment is Mac using PyCharm. Hotmail uses Outlook.

import

First of all, there is something called message from ʻemail, so import smtplibfrom there. smtplib` uses the smtp server to send mail.

from email import message
import smtplib

Setting

I will describe the settings when sending an email.

Specify the server to send the mail to. There is a hotmail called smtp.live.com, so specify it as the host.

from email import message
import smtplib

smtp_host = 'smtp.live.com'

Next, describe the port. This time we will use 587.

from email import message
import smtplib

smtp_host = 'smtp.live.com'
smtp_port = 587

Describe the address of the sender of the email. The e-mail address is [email protected], but you can enter your own e-mail address.

from email import message
import smtplib

smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'

When the sender is completed, write the destination address as to_email.

from email import message
import smtplib

smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'

Next, write ʻusername`. This is the username of your Hotmail account, so your email address will be written as is.

from email import message
import smtplib

smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'

Finally, write the password for your account.

from email import message
import smtplib

smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'

With the above contents, I was able to describe the settings for who to send the mail to and the information for logging in to Hotmail.

message

Compose a message.

There is an object called ʻEmailMessage ()in themessage` that you imported earlier, so use it.

from email import message
import smtplib

smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'

msg = message.EmailMessage()

I will describe the content of the message. Use set_content () to type the body of the email inside ().

from email import message
import smtplib

#Email settings
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'

msg = message.EmailMessage()
msg.set_content('Hello')

Describe the subject, sender, and destination of the email.

The subject will be Subject, the source will be From, and the destination will be To. For the source and destination, use the from_email and to_email variables described earlier.

from email import message
import smtplib

#Email settings
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'

msg = message.EmailMessage()
msg.set_content('Hello')
msg['Subject'] = 'Test email'
msg['From'] = from_email
msg['To'] = to_email

Server settings

We will describe the settings for sending the message described above to the server.

Use the SMTP class of smtplib that you imported to specify the server. Put the host and port inside smtplib.SMTP () and ().

from email import message
import smtplib

#Email settings
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'

#Message content
msg = message.EmailMessage()
msg.set_content('Hello')
msg['Subject'] = 'Test email'
msg['From'] = from_email
msg['To'] = to_email

server = smtplib.SMTP(smtp_host, smtp_port)

As a procedure for connecting to smtp, first use server.ehlo () to connect with the smtp server. I'll call you to exchange from now on.

from email import message
import smtplib

#Email settings
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'

#Message content
msg = message.EmailMessage()
msg.set_content('Hello')
msg['Subject'] = 'Test email'
msg['From'] = from_email
msg['To'] = to_email

server = smtplib.SMTP(smtp_host, smtp_port)
server.ehlo()

After writing server.starttls () to make it secure Try server.ehlo () again.

from email import message
import smtplib

#Email settings
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'

#Message content
msg = message.EmailMessage()
msg.set_content('Hello')
msg['Subject'] = 'Test email'
msg['From'] = from_email
msg['To'] = to_email

server = smtplib.SMTP(smtp_host, smtp_port)
server.ehlo()
server.starttls()
server.ehlo()

Next you will be asked for information to log in with Hotmail Use server.login () to write ʻusername and passwordinside()`.

from email import message
import smtplib

#Email settings
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'

#Message content
msg = message.EmailMessage()
msg.set_content('Hello')
msg['Subject'] = 'Test email'
msg['From'] = from_email
msg['To'] = to_email

server = smtplib.SMTP(smtp_host, smtp_port)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)

Once you are logged in, use send_message () to put the message content you created inside ().

from email import message
import smtplib

#Email settings
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'

#Message content
msg = message.EmailMessage()
msg.set_content('Hello')
msg['Subject'] = 'Test email'
msg['From'] = from_email
msg['To'] = to_email

server = smtplib.SMTP(smtp_host, smtp_port)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.send_message(msg)

At the very end, we will not interact with the server anymore, so use server.quit () to terminate it.

from email import message
import smtplib

#Email settings
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'

#Message content
msg = message.EmailMessage()
msg.set_content('Hello')
msg['Subject'] = 'Test email'
msg['From'] = from_email
msg['To'] = to_email

#Interaction with the server
server = smtplib.SMTP(smtp_host, smtp_port)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.send_message(msg)
server.quit()

Run

Send an email to yourself with the above content If you try it, you can see that the email was actually sent.

image.png

Finally

Associate the program that sends this email with other programs I wish I could do various things.

Recommended Posts

Send email in Python (Outlook)
[Python] Send email
[Python] Send email
[Automation] Send Outlook email with Python
[Python] Send an email with outlook
Send email to multiple recipients in Python (Python 3)
Send email with Python
Send Gmail in Python
Send an email with Excel attached in Python
Send Japanese email with Python3
Read Outlook emails in Python
Send email via gmail with Python 3.4.3.
Python 3.6 email library
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
HTML email with image to send with 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
Plink in Python
Constant 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
Send an email with Amazon SES + 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
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Send email with SES in Python and short message with SMS on SNS
Email attachments using your gmail account in python.
Send an email to Spushi's address with python
Send push notifications to iOS apps in Python
Extract email attachments received in Thunderbird with Python
Send HTTP with Basic authentication header in Python