How to Send Email from a Gmail Account in Python



Python


In this article, we show how to send email from a gmail email account in Python.

With Python, we can send email through any type of third-party email service provider such as gmail, hotmail, yahoo, etc.

In this article, we focus specifically on gmail.

We focus on just one because each email service has a unique way for access. Therefore, each one is individualized. It's not just an all-in-one approach. Each email service has a specific way of accessing it.

That's why we just choose gmail in this article to go over.


Code to Send Email with Gmail

So, below, we have the code to send a simple email message to a person with Python.

We send the message, "Hey, how are you doing? It's great weather today. Want to go to the beach?"





Just to show you what output you should get when you run each line of this code, we show the output below.



So, we'll now go through all of the code and show what output you should get after running certain lines of the code to see if you've executed successfully.

We'll first need to import smtplib, which is the SMTP module needed for this script.

We have to put in the email host name in the script. This is specific to gmail. To send email through gmail, you must specify the host as being, "smtp.gmail.com". Again, this is the way connecting to the simple mail transfer protocol of gmail. The SMTP (Simple Mail Transfer Protocol) is a protocol that is needed to send email through an email service provider.

If you run the line, email_connection.ehlo(), and it returns 'smpt.gmail.com at your service', this means we have successfully connected with gmail.

The next line we must write is, email_connection.starttls()

This line starts a tls (transport layer security), which is a protol that provides communications security over a computer network. Gmail is a secure email service provider, so they utilize tls for security purposes, which provides crytography to the data sent by email.

So, if this line has been successful, we should get the output, 'Ready to start TLS'

We then have to login to the email that we are sending through, in this case, abc@gmail.com. So in order to login, we need the email and password for the account.

We already specified the email and password. So we just have to type in the line, email_connection.login(username, password)

If the email connection has been successful, you should get the line, 'Accepted' as output.

We then send our email through the sendmail() function. from_email is the email we are sending the email through. to_list is the email or the emails that we are sending the email to; this can be one email or 20 emails. And then we have our message.

This is done through the following line, email_connection.sendmail(from_email, to_list, "Hey, what are you doing today? It's great weather today. Want to go to the beach?")

This sends out the email.

If the execution has been successful, you will see, {} as output.


Alternative Way to Send Email

There is a slight alternative way to send emails that is very similar to the above method, except for a subtle change.

This code is shown below.



This code is slightly different because we are importing the SMTP class. So instead of just dealing with the module, we dealing with the SMTP class directly from the module.

We then create a variable called CON and run the same code.

So, again, slight difference and an alternative way of sending emails in Python.

So, this is how an email can be sent with a gmail account in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...