Send Email from your Gmail account in Python

In this article, you will learn how to send email from your Gmail account in Python. This is part of a series of article which I intend to cover on automating boring stuff.

You often come across a number of situation, where you need to send email from your application. For instance, you need to share a daily report to your client. Instead of sending the email manually, we can automate the entire process by sending the email automatically from your Gmail account.

Gmail Security settings

Step 1:  Configure the Gmail security settings to send email via 3rd party.

This is a one-time configuration to configure Gmail to send email via the application (Python application in our case). Gmail does not allow you to use your actual password to be used to send email via the application. So, you need to create something called “App Password”. You can create an app password from your Google account. Please follow the below steps:

  1. Go to your Google Account: https://myaccount.google.com/
  2. Click on Security settings: https://myaccount.google.com/security
  3. Click on App Security and generate an app password
App password in your Gmail account
App password in your Gmail account

Step 2:  Python code to send Email from your Gmail account

We will be using smtplib module to send email via SMTP (Simple Mail Transfer Protocol). You can use the following code to login to Gmail.

server = smtplib.SMTP('smtp.gmail.com', 587) 

context = ssl.create_default_context()

server.starttls(context=context) # Secure the connection

server.login(self.username, self.password)   # Use your Gmail username and App password here

Step 3: Prepare the Email message

Here, we are using MIME module to prepare the email message. We can send the email as a plain text or HTML. In this tutorial, let us consider sending a very simple email having HTML text.

msg = MIMEMultipart()
msg['From']     = self.username
msg['To']       = recipient_email_address  # Single Email address
msg['Subject']  = "Test Email"

msg.attach(MIMEText(email_message, 'html'))

I am assuming that, the recipient email address is a single email account. We can also send email to multiple recipients in which case, each email address needs to be separated by “,”.

For instance, if you need to send email to test1@email.com, test2@email.com, test3@email.com msg object would look like:

msg['To'] = "test1@email.com, test2@email.com, test3@email.com"  # Multiple  Email address

Step 4: The final step is to send the email

server.sendmail(self.username, recipient_email_address, msg.as_string())

Download:

The complete code to send the email from your Gmail account can be downloaded from my GitHub account here: https://github.com/kirancshet/SendGmail-Python

As an exercise, can you explore the following:

  1. Send Email to multiple recipients
  2. Add CC to your email
  3. Add BCC to your email
  4. Attach a simple document with the email

I hope you find this article helpful.

 518 total views,  4 views today

Leave a Reply

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