0% found this document useful (0 votes)
22 views2 pages

SMTP - SSL : There Are Two Ways To Start A Secure Connection With Your Email Server

There are two ways to securely connect to an email server using Python's smtplib module: 1) Using SMTP_SSL() to initiate an encrypted connection from the beginning or 2) Starting an unencrypted connection and then encrypting it using starttls(). Both methods will encrypt the connection using TLS. It is recommended to use smtplib's create_default_context() to validate certificates and ensure a secure connection. Gmail requires port 465 for SMTP_SSL() and port 587 when using starttls().

Uploaded by

Sandeep Rajak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

SMTP - SSL : There Are Two Ways To Start A Secure Connection With Your Email Server

There are two ways to securely connect to an email server using Python's smtplib module: 1) Using SMTP_SSL() to initiate an encrypted connection from the beginning or 2) Starting an unencrypted connection and then encrypting it using starttls(). Both methods will encrypt the connection using TLS. It is recommended to use smtplib's create_default_context() to validate certificates and ensure a secure connection. Gmail requires port 465 for SMTP_SSL() and port 587 when using starttls().

Uploaded by

Sandeep Rajak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

There are two ways to start a secure connection with your email server:

 Start an SMTP connection that is secured from the beginning using SMTP_SSL().

 Start an unsecured SMTP connection that can then be encrypted using .starttls().

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.

Keep in mind that Gmail requires that you connect to port 465 if using SMTP_SSL(), and to port 587
when using .starttls().

Using SMTP_SSL()

The code example below creates a secure connection with Gmail’s SMTP server, using
the SMTP_SSL() of smtplib to initiate a TLS-encrypted connection. The default context of sslvalidates
the host name and its certificates and optimizes the security of the connection.

import smtplib, ssl

port = 465 # For SSLsmtp_server = "smtp.gmail.com"sender_email =


"my@gmail.com" # Enter your addressreceiver_email = "your@gmail.com" #
Enter receiver addresspassword = input("Type your password and press enter:
")message = """\Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()with smtplib.SMTP_SSL(smtp_server,


port, context=context) as server:

server.login(sender_email, password)

server.sendmail(sender_email, receiver_email, message)

Including HTML Content

If you want to format the text in your email (bold, italics, and so on),
or if you want to add any images, hyperlinks, or responsive content,
then HTML comes in very handy. Today’s most common type of email
is the MIME (Multipurpose Internet Mail Extensions) Multipart email,
combining HTML and plain-text. MIME messages are handled by
Python’s email.mimemodule.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy