Send Email in Python using SMTP

By | January 2, 2018

Let us learn how to send email in Python using SMTP (Simple Mail Transfer Protocol) via Gmail and Outlook email client with the complete configuration for the SMTP server.

What is the SMTP Server?

The acronym SMTP stands for Simple Mail Transfer Protocol. This protocol helps to transfer emails from one application to another application using IMAP/POP servers. Find more about how SMTP works here.

Python applications (clients) can use Python SMTP server libraries to send email from a client application using Gmail, Outlook and other email services.

We shall demonstrate how to send email using Python via Gmail and Outlook services. However, it is much easier to send emails in Python using Outlook service as compared to Gmail as the configurations are easier. Therefore, we will suggest you use Outlook email client.

What is SMTPLIB?

Python provides the smtplib module for sending emails. The smtplib module is an SMTP protocol client. It basically defines an SMTP client session object which can be used to send emails with an SMTP or ESMTP listener daemon.

Let us see the syntax to create an SMTP object in Python scripting. We shall be using the same Python smtplib object to send emails in Python using SMTP server.

It is important to define the following two objects while sending emails in Python. Let us have a look at each of them.

  • Host Name: A hostname is a label that is assigned to a machine connected to the Internet. The hostname can take up either the IP address or the domain name of the target system. We have two popular hosts for sending emails in Python via SMTP server mentioned below.
    • smtp.gmail.com
    • smtp-mail.outlook.com
  • Port Number: A port number is a method of identifying a specific process to which an Internet or other network message is to be forwarded when it arrives at a server. We generally use port number 587 for sending emails.
How to send email in Python using SMTP server via Gmail and Outlook email services completely explained

Steps To Configure Google SMTP Server

Step 1: Enable Less Secure Apps feature in your Google account

 

Step 2: Configuring IMAP settings

  • Go to Settings in your Gmail account.
  • Click the Forwarding and POP/IMAP tab.
  • Enable IMAP in the “IMAP Access” section and save the configuration.

Step 3: Configuring Python client code

 
  • Server name: smtp.gmail.com
  • Port: 587
  • Encryption method: STARTTLS

Steps To Configure Outlook SMTP Server

Step 1: Configuring POP and IMAP settings

  • Go to the security settings in the Outlook application. Ensure that POP Options is checked with YES.
  • Once you’re done with enabling POP and IMAP settings, you have to use the following mentioned SMTP server and port number in your Python code to send emails.

Step 2: Configuring Python client code

  • Server name: smtp-mail.outlook.com
  • Port: 587
  • Encryption method: STARTTLS

Note: This python script is written using Python IDLE on the Microsoft Windows operating system and the version is Python 3.6.

Method 1: Send Email in Python using Outlook service

Method 2: Sending emails with attachment

Here, we have written the attached file in text format and placed it in the C drive where Python is installed.

Method 3: Send Email in Python using Gmail service

If you have any issues while writing a script to send email in Python using SMTP server, let us know about it in the comment section below.

More Python Programs

2 thoughts on “Send Email in Python using SMTP

Let's Discuss