[Python] Send email

Thing you want to do

--I want to send an email with python

Background

I wanted to be notified by email of the results of various processes, so I incorporated email sending with Outlook. Since it is a daily operation, if I set it in the task scheduler, it means that an email was sent, but it is not actually sent. It will be sent if you execute it directly, not from the task scheduler. After some research, I found it a bit difficult to use office products such as Outlook in a server environment. I found that I could use the standard library as an alternative, so make a note of it.

point

--Use the standard libraries `smtplib``` and ```email``` --In addition, server host name , port number , authentication ID , authentication password is required --However, it may be OK without ``` authentication ID` and authentication password` `` (depending on the environment) --If there are multiple destinations, they will be sent one by one, so pass the `` `destination address in a list and turn it with the `` `for``` statement.

Code sample

sample.py


from email import message
import smtplib

smtp_host = 'hostname'
smtp_port =Host number
smtp_account_id = 'Authentication ID'
smtp_account_pass = 'Authentication password'
send_from = '[email protected]'
l_send_to = [
    '[email protected]',
    '[email protected]'
]

subject = 'Work report'
content = f'Work is finished'

for s in l_send_to:
    msg = message.EmailMessage()
    msg.set_content(content)
    msg['Subject'] = subject
    msg['From'] = send_from
    msg['To'] = s

    server = smtplib.SMTP(smtp_host, smtp_port, timeout=10)
    server.login(smtp_account_id, smtp_account_pass) #Sometimes it ’s okay without it
    server.send_message(msg)
    server.quit()

Recommended Posts

[Python] Send email
[Python] Send email
Send email with Python
Send Japanese email with Python3
Send email in Python (Outlook)
[Automation] Send Outlook email with Python
[Python] Send an email with outlook
Send email via gmail with Python 3.4.3.
HTML email with image to send with python
Send email to multiple recipients in Python (Python 3)
Send an email with Amazon SES + Python
Send email with Django
Validate E-Mail with Python
Send Gmail in Python
Send an email to Spushi's address with python
Send an email with Excel attached in Python
Python
Send email using Python's smtplib
Send using Python with Gmail
[Python] Send an email from gmail with two-step verification set
[boto3] Send an email using SES
Easy email sending with haste python3
Note: Send an email with Django
Send multipart / form-data with python requests
Send image with python, save with php
[Python] Create API to send Gmail
kafka python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python comprehension
Python technique
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python tips
python function ①
Python basics
Python memo
ufo-> python (3)
Python comprehension
install python
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
What to do if you couldn't send an email to Yahoo with Python.
Python increment
atCoder 173 Python
Send an email to a specific email address with python without SMTP settings
[Python] function
Send a message from Python to Slack
Python installation
Installing Python 3.4.3.
Send email with SES in Python and short message with SMS on SNS
Try python
Python memo
Python iterative