When it is troublesome to set up an SMTP server locally when sending mail with Python.

A script that sends emails in Python. Since it is troublesome to set up an SMTP server locally, How to use SMTP on the rental server.

mail.py



#!/usr/bin/env python
# -*- coding: utf-8 -*-
import smtplib
from email.MIMEText import MIMEText
from email.Header import Header
from email.Utils import formatdate

FROM_ADDR = "Sender email address"
TO_ADDR = "Destination email address"
ENCODING = "iso-2022-jp"

message = MIMEText(
	u"Has completed".encode(ENCODING),
	"plain",
	ENCODING,
	)

message["Subject"] = str(Header(u"Notification e-mail",ENCODING))
message["From"] = "%s <%s>" %(str(Header(u"From",ENCODING)),FROM_ADDR)
message["To"] = "%s <%s>" %(str(Header(u"To",ENCODING)),TO_ADDR)
message["Date"] = formatdate()

s = smtplib.SMTP("SMTP server", 587)
s.ehlo()
s.starttls()
s.login("SMTP login account", "password")

s.sendmail(
	FROM_ADDR,
	[TO_ADDR],
	message.as_string(),
	)
	
s.close()

print "Success send mail"

I think it can also be used to send an email when something is done.

Recommended Posts

When it is troublesome to set up an SMTP server locally when sending mail with Python.
Send mail with mailx to a dummy SMTP server set up with python.
How to set up a simple SMTP server that can be tested locally in Python
When it is troublesome to copy what you built with vue
Set up a test SMTP server in Python.
Set up a simple SMTP server in Python
It is troublesome to change the settings between the intranet and business trip / at home, so I was a little happy when I set up a forward proxy locally with Apache2.
[Vagrant] Set up a simple API server with python
After all it is wrong to cat with python subprocess.
Set Tomcat to restart automatically when it raises an OutOfMemoryError
Reload the server set up with gunicorn when changing the code
Precautions when inputting from CSV with Python and outputting to json to make it an exe
[Python] What is a slice? An easy-to-understand explanation of how to use it with a concrete example.
NameError: global name'dot_parser' is not defined and what to do when it comes up in python
Don't write Python if you want to speed it up with Python
Set up a dummy SMTP server in Python and check the operation of sending from Action Mailer
Set up a yum repository server on CentOS7 system and refer to it locally and from other servers.
How to write what to do when an application is first displayed in Qt for Python with Designer
Introduction and usage of Python bottle ・ Try to set up a simple web server with login function
When you want to use it as it is when using it with lambda memo
It is better to use Weight Initializer when initializing with HeNormal with Chainer
Send an email to a specific email address with python without SMTP settings
Set up reverse proxy to https server with CentOS Linux 8 + Apache mod_ssl
[Python] What to do when an error related to SSL authentication is returned
Read CSV file with Python and convert it to DataFrame as it is
Set up a Samba server with Docker
Note when creating an environment with python
I tried sending an email with python.
Set up a mail server using Twisted
Introduce postfix to WSL to relay mail addressed to Office365 and process it with Python
I tried to make a simple mail sending application with tkinter of Python
It is more convenient to use csv-table when writing a table with python-sphinx
When I tried to create a virtual environment with Python, it didn't work
[OpenCV] When you want to check if it is read properly with imread
[Linux] SMTP setting method in Linux environment (host) when sending notification mail with Docker
What to do if you get an error when installing python with pyenv
[What is an algorithm? Introduction to Search Algorithm] ~ Python ~
Set up a simple HTTPS server in Python 3
Set up a simple HTTPS server with asyncio
Set up a local server with Go-File upload-
ODBC connection to FileMaker 11 Server Advanced with Python 3
Send an email to Spushi's address with python
Python log is not output with docker-compose up
How to crop an image with Python + OpenCV
Set up a local server with Go-File download-
How to set up a local development server
I tried sending an email with SendGrid + Python
Post an article with an image to WordPress with Python
Materials to read when getting started with Python
Create an API server quickly with Python + Falcon
[Python] Type Error:'WebElement' object is not iterable What to do when an error occurs
[Python Data Frame] When the value is empty, fill it with the value of another column.
What to do if you get an OpenSSL error when installing Python 2 with pyenv
Investigate the cause when an error is thrown when python3.8 is not found when using lambda-uploader with python3.8
It is better to use NTFS when connecting SSD to Linux to create a file server.
[Python] What to do when No module named'pyproj.datadir' appears when Exe is done with PyInstaller
Set up an FTP server that can be created and destroyed immediately (in Python)
[Python] Precautions when it does not work even if TimedRotatingFileHandler is set in basicConfig in python2
[Introduction to Python] What is the important "if __name__ =='__main__':" when dealing with modules?
python note: What does it mean to set a seed with random number generation?
It is easy to execute SQL with Python and output the result in Excel