Python Send Email Using SMTP

In this tutorial we will see how to send email using Python. We will need two python libraries for the same, smtplib will be used for making a host for sending email it will also handle routing of email between different servers. email package allows us to use HTML and other kind of text in our email, we can also use attachments using the same. 

For sending email we will first need to create a local SMTP (Simple Mail Transfer Protocol) server. This can be done by creating an SMTP object from smtplib. Connections from SMTP server are considered as less secure by Gmail so you need to first allow less secure apps from setting for this to work. But this will also allow other less secure apps to use your Gmail so it is highly recommended that you create a new gmail account for this purpose. 

Python Send Email Using SMTP

Let us first create an SMTP object for sending email.

Here smtp.gmail.com is our host address and 587 is port number, here we are using the TLS (Transport Layer Security) encryption which will create an unsecured connection which is being encrypted using .starttls() later. If we use SSL (Secured Socket Layer) Protocol then we should use port 485. We will be sticking with TLS protocol in this tutorial.

Now our local server is set up and we are ready to create the body of our email. We will send MIME (Multipurpose Internet Mail Extension) Multipart email, these are handled by the email.mime module of email package.

Documentation for the email mime module can be found here. We will start by first creating a MIMEMultipart object which is basically an email object. It will have all the attributes like From, To, Subject, Email body and other email attributes. We will fill all these fields and we are good to go. The text which is going to present on must be converted into a MIME object which is done using the email.mime.text module.

The above code will set the from, to and subject field in our email. For the body of email we will create a string and convert that into MIMEText object and then attach that to MIMEMultipart object.

The MIMEText takes two arguments first is the text and the second is the subtype of that text, here we are having plain text therefore our subtype is plain.

Only part now remaining is to login into the SMTP server and send the mail. We can login using the from field of msg as we are going to send our msg from that email address.

The complete is as below : 

Note : Sometimes specific ports are blocked on public internet so if you face TimeourError then try changing your internet connection.

Till now we have seen how to set up local server and send plain messages. Let us now move to how we can send different styles messages and use attachments. Now we will see how can attach html content in email.

Everything else will remain the same we just need to change the message string to a HTML text and while converting the string to MIMEText object we should specify that the subtype as html. We can attach multiple time to our Multipart object.

Now we will move to attachments how attachments can be done in email.

Attaching Image:

We should first open our image using in python convert that into an MIMEImage object and then attach it to MIMEMultipart object.

Make sure that you add extension of image while is being used in path of image. The above code will attach image to our mail.

Attaching PDF:

Let us now see a code having all the above functionalities:

Sending Personalized Emails:

Till now we have seen sending general emails to receptionist but this is not of much use, In most of the real case scenario we will need to send personalized emails. We can do this easily just by changing a few things in our code. We will first save all the information into a csv (Comma Separated Values) file. 

For example here, suppose we have a .csv file containing name, email address, marks and grades of students and we want to send emails to all of them containing their marks and grades. We will first create our msg and then we can format it using the .format method in python before sending the mail.

Here att.format is formatting our message by taking values from the csv file. This way we can easily send personalized messages to a list of receptionists. 

For more information on MIMEText, MIMEImage, MIMEMultipart and functions .sendmail(), .attach() you can visit the documentation here.

Comment down below if you have queries related to python send email using smtp.

Leave a Comment

Your email address will not be published. Required fields are marked *