Python Email Automation using Yagmail Module

The Simple Mail Transfer Protocol (SMTP) is a protocol used by email services for sending emails. This gives us the idea that yagmail will have som...

Do you know, What is a robot? 🤭

If you are reading such blogs off course you know about it. So what Wikipedia says, Robot is a machine programmed to do complex tasks automatically. and automatically means without any human interaction. So if you follow this tutorial, this will be one of the first few steps for you towards robotics because here we are going to discuss how to automate emails just like robots do.

So let's dive into the tutorial! 🤿

Python Email Automation

Requirements

There is a requirement for everything you want to do. So for this mini-project, you will need the following things and how to get them? No problem I have all the answers for you.

  • Knowledge of Python Programming Language.
  • A PC with python 3.x installed in it.
  • A good code editor.
  • A Gmail Account with credentials.

So these are the basic requirements for this project. You must have a piece of programming knowledge in python, and for that, there are many resources available on the internet. To download python visit python.org. vs code or sublime is a good solution as a code editor and everyone has a Gmail account these days.

After all the above you have one more thing left. It's the yagmail module. A python module to make Gmail automation with python an easier task for everyone.

About Yagmail

The Simple Mail Transfer Protocol (SMTP) is a protocol used by email services for sending emails. This gives us the idea that yagmail will have something to do with it.

For your knowledge, Yagmail is a python package that helps in automating Gmail. Yagmail stands for Yet Another Gmail SMTP client.

The official documentation for yagmail is available on the pypi.org website. pypi.org can be said the Wikipedia for the python packages.

Installing the Package

To install a python package you need the pip package manager, the packager manager for python installed with python.

So just open the command prompt and type the following command and hit enter.

pip install yagmail

this will install the yagmail and other dependencies for it.

Creating an SMTP client

To write something we must open the notebook first. Like that, Here we will create an SMTP client which connects to our Gmail account with the provided USERNAME and PASSWORD.

Now open your favourite code editor and start coding in a .py file.

An SMTP client can be created using the following code and then we store it into a variable let sender here.

sender = yagmail.SMTP(user = 'USERNAME', password = 'PASSWORD')

Now we can access it using the sender variable in which it is stored.

Sending A Mail

After creating an SMTP client we can send emails to whichever email id we want, Just like we usually do in Gmail or any other mail service.

Now to send mails the SMTP client we just created has a send function that requires some arguments to send mails. The required arguments are as follows.

  • receivers mail id
  • subject
  • contents etc.

These are the common things we need every time for sending an email. For automating the same process these things are necessary or it will not work.

Now add this code to your file.

recipient='ziron@gmail.com'
subject="Welcome to devtejas"
contents=["Wake up ziron...", "There is a new post on devtejas"]

sender.send(to=recipient, subject=subject, contents=contents)
In the above code the variables recipient, subject and content respectively store our required arguments.

The end line uses the send function to send the email.

Sending to Multiple Recipients

To send the mail to multiple recipients we can store their emails in a list name recipient as shown below.
recipients=['ziron.gmail.com','ziron@matrix.com','tejas@devtejas.me']
subject="Welcome to devtejas"
contents=["Wake up ziron...", "There is a new post on devtejas"]
sender.send(to=recipients, subject=subject, contents=contents)

Add an Attachment

Attachments can be added to the mail in two ways, 
  1. Providing the full path to the attachment in contents or
  2. Providing the path to the attachment as an argument to attachments parameter.
recipients=['ziron@gmail.com']
subject="Welcome to devtejas"
contents=["Wake up ziron...", "There is a new post on devtejas", 'devtejas.png']
sender.send(to=recipients, subject=subject, contents=contents, 
            attachments='post.pdf')

The above code adds a picture devtejas.png as an attachment to the mail if it is available in the directory where the code exists. Also note that a pdf file, post.pdf is added using the attachments parameter.

Adding BCC and CC

You must have seen there are two more options when writing a mail below the to: column. that is BCC and CC.🤔

You can google their meanings. Here we will focus on how to add them to our Gmail automation process.

So just like the attachments and other things BCC and CC to mail can be added by using BCC and CC parameters within the send function.

recipients=['ziron@gmail.com']
subject="Welcome to devtejas"
contents=["Wake up Ziron...", "There is a new post on devtejas", 'devtejas.png']
sender.send(to=recipients, subject=subject, contents=contents, 
            cc='ziron@matrix.com', bcc='tejas@devtejas.me')

This is how you can automate the process of sending emails to someone.

And by applying some advanced logic to your program you can make a much better version of this mini-project.

Try experimenting with this module. Read its documentation. And don't forget to comment if this post helped you.

Thanks!!

There was an error in the code now it is resolved
Last Updated on: 18 July, 2021

Keep Coding Coders!