How to Automate Your Emails With Python

How to Automate Your Emails With Python

May 16, 2020 big data programming 0
automation python programming

Automating daily tasks with Python is easy. With a combination of APIs and easy-to-understand libraries, you can easily set up systems that scrape websites, send emails, and manage data and analytics.

One very common task you’ll need to automate in the corporate world is the ability to scrape public government data. This usually comes from sites like data.gov and other end points to get information on healthcare, trade, transportation, legal, and so much more.

There are actually a lot of government agency sites that are somewhat hidden that are still producing a lot of valuable data that billion dollar companies rely on to make million-dollar decisions.

In this article, we’ll outline how you can write a Python script that can download a specific set of government data and then upload that to an email and send it to whomever you want.

This will get you comfortable with using the Python Requests library as well as using the Gmail API. So if you’re looking to automate future emails with Python, this is a great place to start.


Downloading the File With Python

For step one, we will need to actually download the data file using an HTTP request.

What’s great about Python is much of the complexity of writing code like HTTP requests has been removed. Instead, you can import the Requests library and use it to post and get requests.

In this case, the file we’ll be downloading even has an easy end point at the link below. So you won’t need to use a library like Selenium to try to click a download button.

Even if there’s a download button, you should consider checking if it has an URL attached. Typically, if there’s an URL, then it’s really easy to download using a link versus creating a web crawler that looks for a button to click.

https://data.medicaid.gov/api/views/u72p-j37s/rows.json?accessType=DOWNLOAD

This file provides information about Medicaid and individual enrollees. This can be valuable to healthcare providers that can tie it to their internal data to help better understand their market.

In order to download it, we’ll use the function requests.get(). This will let us pull data using an HTTP request to the URL we specify.

For example, you can look at the script below:

It’s short and sweet and will return the CSV as part of the request you’ve now set as the variable. We’ll use this later when we build the email. But next, we need to set up the credentials for the Gmail API.


Setting Up Your Gmail API

Google has made it really easy for you to set up APIs. You can go to the Google API console. From there, you can select ENABLE APIS AND SERVICES and then search for the Gmail API.

The API console looks like the image below.

After clicking that button, you’re provided with a search engine that allows you to search various APIs. It’ll look like the image below.

You can type in Gmail, and it should be the only one that shows up.

Then you can select the Gmail API, and it’ll have an ENABLE button by it.

Once you click ENABLE on the Gmail API, you can download your credentials or use an API key and secret.

Our code will use the JSON download but then convert it into a pickle if that’s what your prefer.

With that all set up, we can now start building your functions to set up and then send your email automatically.


Sending Emails With The Gmail API

Now that we’ve found a way to pull this data, we need to figure out how to actually send an email.

In order to do this, we’ll use the email library. This library will let us set up the various parts of en email: the sender, the recipient, the subject, etc.

We do this using the [MIMEBase](https://docs.python.org/2/library/email.mime.html) class in email, which makes it easy to set up the correct data points and provides an easy class for the Gmail API to use in the future to parse.

It’s really simple to use the MIMEBase class, as you can initiate a new class and then reference many of the needed components like:

message[‘from’] = test@gmail.com

You can see the entire function where we set these parameters below.

You’ll notice at the end, we use the function urlsafe_b64encode. This will set the message to bytes. This will be used to easily transfer the email data to the Gmail API. So it’ll be easy to pass along.

Now it’s finally time to send your first automated email.


Sending Your First Automated Email With Python

Now that you’ve set up your Gmail API credentials, we can send our first email.

We’ll use the service variable we’ve set up with the Gmail API and credentials.

This is shown below in the function send_message.

From here, all we need to do is pass in the message and execute.

With that, we’ve sent our first email.

The whole script looks like the code below.


Time to Start Automating

We hope this script helps you automate future emails with Python as well as look for other tasks you can automate.

In the examples above, we’re manually scheduling this script. However, in the future, if you wanted to automate this more effectively, you could use a framework like Airflow. Then instead of manually running the script, you could instead run this script on a daily basis.

We don’t recommend trying to build your automation framework, especially when there are so many great options out there. That’s why we really just show the script.

With that, good luck and happy automating!

If you would like to read more about data science or cloud computing, then please click below.

Kafka Vs RabbitMQ

Data Engineering 101: Writing Your First Pipeline

Data Engineering 101: An Introduction To Data Engineering

What Are The Different Kinds Of Cloud Computing

4 Simple Python Ideas To Automate Your Workflow

4 Must Have Skills For Data Scientists

SQL Best Practices — Designing An ETL Video

5 Great Libraries To Manage Big Data With Python

Joining Data in DynamoDB and S3 for Live Ad Hoc Analysis