Sending Electronic Mails With Python
Software Engineer | Full Stack Web developer | Javascript | React.js | Python | PHP | AWS | Docker
Sending emails is done with Python's smtplib module using an SMTP server. First of all, we obviously need an e-mail address. In this tutorial, we will discover how to send an email with Python using the smtplib module. Python includes several modules in the standard library for working with email and mail servers.
There are tools like Prometheus that allow you to define these alerts and the behavior to adopt, but it can also be interesting to know how to send an e-mail with the basic Python libraries.
First of all, we need an email address. This is hosted by a provider who can provide us with information about the SMTP server (Simple Mail Transfer Protocol): an address and a port (usually 465 or 568).
For instance:
Gmail: smtp.gmail.com YahooMail: smtp.mail.yahoo.com Outlook: smtp.office365.com
Behind it, just connect to our SMTP server using Python's smtplib and ssl libraries:
import smtplib, ssl
# we enter the information taken from the supplier's site
smtp_address = 'smtp.gmail.com'
smtp_port=465
# we enter the information on our e-mail address
email_address = 'example@gmail.com'
email_password = 'my_password'
# we enter the information on the recipient
email_receiver = 'another.example@yahoo.com'
# we create the connection
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_address, smtp_port, context=context) as server:
# account login
server.login(email_address, email_password)
# send email
server.sendmail(email_address, email_receiver, 'email content')
It's not more complicated than that. So of course, the e-mail is not very beautiful and has no subject, but we have the basics...
Now let's try to have a slightly more polished email using HTML. For this, we will use the email library:
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# create an email
message = MIMEMultipart("alternative")
# add a subject
message["Subject"] = "[DataScientest] test email"
# an emitter
message["From"] = email_address
# a recipient
message["To"] = email_receiver
# we create a text and its HTML version
text = '''
Hello
My awesome newsletter
cdt
my_incredible_link
'''
html='''
<html>
<body>
<h1>Hello</h1>
<p>My awesome newsletter</p>
<b>Cdt</b>
<br>
<a href="https://datascientest.com">my_amazing_link</a>
</body>
</html>
'''
# we create two MIMEText elements
mime_text = MIMEText(text, 'plain')
html_mime = MIMEText(html, 'html')
# we attach these two elements
message.attach(mime_text)
message.attach(html_mime)
# we create the connection
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_address, smtp_port, context=context) as server:
# account login
server.login(email_address, email_password)
# send email
server.sendmail(email_address, email_receiver, message.as_string())
It's not much more complicated than that. That said, this is no reason to get into scams or spam.
Sending emails is certainly not the tool most frequently used by Data Scientists and Data Engineers, but sometimes it turns out that it is the easiest way to notify that an event has happened.
