Sending Emails in Python using SMTP (Tutorial)

Sending Emails Using Python

Follow this step by step tutorial to send emails with Python. Learn to do the SMTP settings and other configurations to send a text or HTML based email.

How To Send Emails in Python?

As many of your might already know that the SMTP (Simple Mail Transfer Protocol) is used to send and route the emails between the different email servers. Therefore before we can actually start sending emails using any of the programming languages, we must know a little bit about SMTP and how to configure it.

Python comes with a built-in module smtplib that help us manage the SMPT configurations. This module allows us to create an SMTP Client Session Object. This object is further used to implement the configured settings and to send to any of the other email servers that are connected to the Internet and have the SMTP or ESMTP listener daemon enabled.

Simplest Code To Send Emails in Python

You just need to run the following lines of code in python to send a simple email using any SMTP Server.

The above code will send a simple PLAIN TEXT email without any subject and other email parameters.

Let’s quickly understand each line of this code. First of all, we’ve imported th smtplib module. Then to establish the SMTP connection, we’ve used the method smtplib.SMTP() and passed the SMTP server address as the first argument and the port number as the second argument. The SMTP server address is a string type argument while the port number is a positive integer type argument. After creating the session, I referred it by the variable named s.

In my code, I’ve used the Gmail SMTP server. Therefore, you can replace smtp.gmail.com with your SMTP Server address. At port 587, the Gmail SMTP server provides the TLS Security while at the port number 465 most of the SMTP Servers provides the SSL Security.

After this, you can start the session by using the starttls() method if you’re using the TLS port.

Next, you have to specify the credentials for your SMTP email account. If you are also using Gmail, then simply enter your Gmail id and password as the first and second arguments of the login() method referred by the SMTP variable s that we’ve early created.

Then I’ve created a multiline string in which I’ve entered my email body message and saved it into the message variable. Afterwards, I’ve simply used the sendmail() method to actually send the email. You must provide at least three arguments to this method and these arguments are the sender’s email address, receiver’s email address and the message itself. All of these are string type arguments.

That’s it, now this code is ready to send the email to the receiver’s email address and now you can use the method quit() to terminate the SMTP session.

Here’s a screenshot of the email that is sent using the above code from the receiver’s viewpoint.

Simplest Code To Send Emails In Python

As you can see, there’s no subject as we’ve not defined it anywhere in the code.

Sending Emails with Subject in a standard format

The code illustrated in the above section is the most basic one and there are a lot more things that we can actually do draft a proper email in a standard format. That’s all we are going to learn in this section.

The following Python code sends an email with the subject.

In the above code, we’ve used some of the built-in Email Library Functions. The basic idea of working is the same as the code mentioned earlier in the previous section. It’s just that here we’re following a different approach to create the email body and to send it.

First of all, to create the email body, we’ve used the MIMEmultipart() function to create the email body in different parts such as From, To and Subject. Then we’re stored our message string into the message variable. To attach this message with the email we have used the method attach() along with the MIMEText method to implement the message as plain text. In the end, the send_message() method of the SMTP session object is used to finally send the email.

The email sent using this code is viewable as shown in the following screenshot from the receiver’s perspective.

Sending Emails With Subject

You can clearly observe that now the email has the subject.

Sending HTML Emails in Python

We’re sending plain text emails as of now and this can be proven with the fact that the h1 tag that we’ve used in the email content is not being rendered. To render HTML code within the email, you just have to do a minor change in the code of the above section.

Simply change the second argument of the function MIMEText from 'text' to 'html' in the Line No. 29 of the above section code.

Sending HTML Emails Using Python

As you can see in the screenshot the HTML code is now rendered. The H1 Tag can now be identified as different in font-size from the other email body text.

Adding Attachments To Your Email

Sending files as attachments is one of the most basic necessities. In this section, I’ll quickly teach you to attach different sort of files and send them along with your email in Python.

Given below is the Python Code that sends the email along with an attachment file named demo.pdf.

As you can see in the following screenshot, the receiver got the attachment file named demo.pdf in the email.

Sending Email Attachments Python

Similarly, you can also attach multiple files to your email. You can create a Python List containing the file names with their full paths and then can loop through that list using for loop.

Related Articles.

I hope you found this guide useful. If so, do share it with others who are willing to learn about the different topics that we publish here on our blog. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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