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