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

Python is an interpreted high level multi-paradigm scripting language which has a lot of scope in web development. You should know what is Python used for in real-world to understand the applications of Python language in a better way.

What is Python SMTP Server?

The acronym SMTP stands for Simple Mail Transfer Protocol. This protocol helps to transfer mails from one to another 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 more easier to send emails in Python using Outlook service as compared to Gmail as the configurations are easier. Therefore, we will suggest you to use Outlook email client.

For sending an email, Python provides smtplib module. The smtplib module is a 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 host name is a label that is assigned to a machine connected to the Internet. The host name 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 the port number 587 for sending emails.
How to send email in Python using SMTP server via Gmail and Outlook email services completely explained send email in python using smtp [complete guide] - Send Python Email using SMTP - Send Email in Python using SMTP [Complete Guide]

Steps To Configure Google SMTP Server

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

Step 2: Configuring IMAP settings

  • Goto 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 Windows operating system and the version is Python 3.6.

Method 1: Send Email in Python using Outlook service

Method 2: Send Email in Python with attachment using Outlook service

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.

Source link


Please enter your comment!
Please enter your name here