Python Sending Email Tutorial using SMTP Protocol

Hello friends, welcome to new tutorial  Python Sending Email Tutorial, in this tutorial we will discuss about how to send email from python.

In most of applications, we need to communicate with our users by sending messages electronically.Email is used to send password resets, confirmation of orders, and verification of user accounts etc.Email works with SMTP(Simple Mail Transfer Protocol) protocol which handles sending e-mails and routing e-mails between mail servers.

Get Started Python Sending Email Tutorial

Python Sending Email Module

Python provides smtplib for sending emails. The smtplib module defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.

Importing smtplib

Now let’s start coding part.

Create a new project , named it as sending_email or as you like. I am using PyCharm IDE , you can use anything.Now create a new python file by following the path- sending_email->Right click->New->Python File and named it as you wish.

Requirements for sending email

  1. SMTP domain name of service providers with port number.
  2. Sender’s username and password.
  3. Receiver’s email address.

E-mail service providers and their SMTP domain names

  1. Gmail – , 587
  2. Yahoo – , 587

Here I am using Gmail.

So let’s start to implement our program

Sending a Simple Email

Now run the above code. While running we get a error that is a security error.

To solve this error we just need to go to My Account->Sign-in & Security->Apps with Account Access. After scrolling down we see a dialog box as below

Now this is off, you have to turn it on. Again run the code now there is no error and email sended successfully to the receiver.

On clicking this email we can see the format of email, it is quite simple.

email with Subject

Now we attach subject in our email. For this we modify our code little bit as below

Now check the email, and you will see the email received in your email.

email with attachment

Now we attach an attachment in our email. For this we modify our code as below.

  • On running above code we get following output.

email with image

It is same as sending a file, and you can attach any file to your email and you can attach an image file as well.

The output will be as follows :


So that’s all for this  Python Sending Email Tutorial friends. I hope you found it helpful if you did, then please SHARE this post with your friends who are learning python.

And yes, if you have any query regarding this Python Sending Email Tutorial then your questions are welcome. Thank You 🙂

5 thoughts on “Python Sending Email Tutorial using SMTP Protocol”

  1. I facing problem in emali sending tutorials.I m also message you on fb & comments on ‘Sending email’ post on fb..So please solve my problem


Leave a Comment