Yagmail: How to send emails using Python!

Yagmail: How to send emails using Python!

We’ll practically learn how to send emails using Python today using the Yagmail module.

It might be important to note that the yagmail module may be restricted from gmail access as a less secure app.

In that case, you can allow access by modifying the setting from myaccount.google.com/security > Less secure app access.

Table of Contents

Introduction to Yagmail – An Easy Module to Send Emails using Gmail in Python

The yagmail module is a simple Gmail/SMTP Client that was created in order to send emails with relative ease from a Python Script.

It quite literally stands for Yet Another GMAIL/SMTP Client, quite the straightforward name.

This module simplifies the task of using the web browser or mail application in order to send emails, allowing for a terminal based interaction instead.

We’ll only need to provide a few fields, and then we can send an email in a matter of minutes.

Installing Yagmail to send emails using Python

In order to work with yagmail module in your Python Script, we’ll first need to install it using a package manager.

With the pip manager, we can install the yagmail module through this command below,


pip install yagmail

Now, that we’re done with that, we can get started with using the yagmail module in a script, so, let’s get to it.

Steps to send emails using Python

Now, let’s go over the steps to send your first email using Python! In order to send emails using Python, we’ll first need to import the yagmail module into the script,


import yagmail

Getting past this step with the above command, let’s get started working using the components provided by the module.

This section is divided into three sections, which can help us work with the different aspects of the yagmail module to begin sending emails using Python!

1. Registering the user

Registering a user for utilization of the SMTP client is important, as this what provides authentication to the client in order to send an email to the receiver.


yagmail.register('mygmailusername', 'mygmailpassword')

This allows yagmail to access your email account in order to send emails using Python.

This essentially acts as a wrapper for another tool known as keyring, which provides a prompt asking for the password in case the field is left empty.

You can also choose to create a .yagmail file, in the same folder, helping in specifying the email username, preventing exposure in the code.

2. Using the SMTP Client

In order to start a connection, we use the command,


yag = yagmail.SMTP('mygmailusername')

We can also define a few variables that can be used during the delivery of the email, specifying the recipients, subject, and body.

A few examples of using these would be these fields,


to = '[email protected]'
to2 = '[email protected]'
to3 = '[email protected]'
subject = 'Quite the subject line'
body = 'Pretty sure this is the body.'

However, all these fields are optional and in case there’s no to, you’ll be sending an email to yourself.

A feature that the knockknock module implements very well.

3. Content and Delivery

As you’ve seen before, the contents can be defined through the optional fields above.

However, the method to send the emails, will require them to be placed into a function.

This should look something like this,


yag.send(to = [to, to2, to3], subject=subject, contents=body)

You’re really done after this, just hit back and relax while your program does the work for you from now on, with a bit of input on your side perhaps.

4. Sending emails using Python

Here’s an example of an email being sent to someone about an interesting fact!


# Importing yagmail and it's components
import yagmail # Used when you're in the interpreter rather than script
# Adding in the username and password
# yagmail.register("[email protected]", "gmailpassword") # Starting a connection with the SMTP Client
yag = yagmail.SMTP(user='[email protected]', password='gmailpassword', host='smtp.gmail.com') # Adding in the details
toRec = "[email protected]"
subjectLine = "Quite intriguing"
body = ["You can't buy Coca-Cola in North Korea"] # Delivery
yag.send(to=toRec, subject=subjectLine, contents=body)
print("Email sent successfully")

Moving Forward

Many times, you wouldn’t want to send the same emails to the same people.

And, that would mean that you’d have to go into the code and edit it every time.

Instead of doing that, it would be a better solution to provide an input field for the user to type down the information with a simple prompt.


body = input("Enter the contents of the email: ")
yag.send(contents=body)

The features of the yagmail module don’t end there, it also provides a method for OAuth2 verification, as a measure of security.

In case you’re interested in that, it might be a good idea to look into their GitHub documentation regarding the OAuth2 Token usage.

Yagmail has been extended and utilized in a few other modules as well. A prominent one is the knockknock module which can be found here.

Conclusion

Working with yagmail is a great way to extend your application’s limits, with features to send either you or anyone an email regarding a particular automated task in your script.

This also works when you’re really not in the mood for some GUI components in the web browser.

Plus, it also helps in scripting a simple email quickly and easily!

Look into some of our other articles on interesting topics like Regular Expressions, Docstrings, and the inspect module.

References