Sending Emails With Python - The Apps Blaster
Sending Emails With Python - The Apps Blaster
Python Projects
There are two ways to start a secure connection with your email server:
https://theappsblaster.com/?p=7386 1/6
5/10/22, 0:02 Sending Emails With Python – The Apps blaster
In both instances, Gmail will encrypt emails using TLS, as this is the more
secure successor of SSL. As per Python’s Security considerations, it is highly
recommended that you use create_default_context() from the ssl module.
This will load the system’s trusted CA certificates, enable host name checking
and certificate validation, and try to choose reasonably secure protocol and
cipher settings.
If you want to check the encryption for an email in your Gmail inbox, go
to More → Show original to see the encryption type listed under
the Received header.
smtplib is Python’s built-in module for sending emails to any Internet machine
with an SMTP or ESMTP listener daemon.
I’ll show you how to use SMTP_SSL() first, as it instantiates a connection that is
secure from the outset and is slightly more concise than
the .starttls() alternative. Keep in mind that Gmail requires that you connect
to port 465 if using SMTP_SSL() , and to port 587 when using .starttls() .
context = ssl.create_default_context()
https://theappsblaster.com/?p=7386 2/6
5/10/22, 0:02 Sending Emails With Python – The Apps blaster
server.login("my@gmail.com", password)
It’s not safe practice to store your email password in your code, especially if
you intend to share it with others. Instead, use input() to let the user type in
their password when running the script, as in the example above. If you don’t
want your password to show on your screen when you type it, you can import
the getpass module and use .getpass() instead for blind input of your
password.
The code snippet below uses the construction server = SMTP() , rather than
the format with SMTP() as server: which we used in the previous example. To
make sure that your code doesn’t crash when something goes wrong, put your
main code in a try block, and let an except block print any error messages
to stdout :
smtp_server = "smtp.gmail.com"
sender_email = "my@gmail.com"
context = ssl.create_default_context()
try:
https://theappsblaster.com/?p=7386 3/6
5/10/22, 0:02 Sending Emails With Python – The Apps blaster
server = smtplib.SMTP(smtp_server,port)
server.login(sender_email, password)
except Exception as e:
print(e)
finally:
server.quit()
To identify yourself to the server, .helo() (SMTP) or .ehlo() (ESMTP) should
be called after creating an .SMTP() object, and again after .starttls() . This
function is implicitly called by .starttls() and .sendmail() if needed, so
unless you want to check the SMTP service extensions of the server, it is not
necessary to use .helo() or .ehlo() explicitly.
I recommend defining the email addresses and message content at the top of
your script, after the imports, so you can change them easily:
sender_email = "my@gmail.com"
receiver_email = "your@gmail.com"
message = """\
Subject: Hi there
https://theappsblaster.com/?p=7386 4/6
5/10/22, 0:02 Sending Emails With Python – The Apps blaster
smtp_server = "smtp.gmail.com"
Subject: Hi there
context = ssl.create_default_context()
server.login(sender_email, password)
For comparison, here is a code example that sends a plain-text email over an
SMTP connection secured with .starttls() . The server.ehlo() lines may be
omitted, as they are called implicitly by .starttls() and .sendmail() , if
required:
smtp_server = "smtp.gmail.com"
sender_email = "my@gmail.com"
receiver_email = "your@gmail.com"
message = """\
Subject: Hi there
context = ssl.create_default_context()
server.starttls(context=context)
server.login(sender_email, password)
https://theappsblaster.com/?p=7386 5/6
5/10/22, 0:02 Sending Emails With Python – The Apps blaster
https://theappsblaster.com/?p=7386 6/6