Extract content from messages in Gmail

Introduction

In this tutorial, you’ll learn how to read through an inbox full of receipts, pull out dollar amounts, and download it into a spreadsheet whenever you want.

Specifically, you’ll learn how to:

  • Create a connection to Gmail
  • Create a function that reads through your emails and extracts dollar amounts from email receipts
  • Convert your extract email receipt info into a spreadsheet
  • Download the receipt info

Setup

To complete this tutorial, you’ll need the following:

  • A Flex.io account
  • A Gmail account with email receipts you’d like to extract

Build it

To create this pipe, we’ll be using the Flex.io web interface. Sign in to Flex.io, or sign up if you don’t yet have an account.

Connect to Gmail

Next, create a connection to Gmail:

  1. Select “Connections” from the list on the left.
  2. In the list of connections, click on the “New Connection” button in the upper-right of the screen.
  3. In the “New Connection” dialog, select the Gmail connection.
  4. In the connection configuration, name the connection “Gmail” and give it an alias “my-gmail”.
  5. Next, authenticate the connection.
  6. Finally, click on “Create Connection” to connect to Gmail.

Extract receipt info

Next, create a pipe to read through your emails and extract the receipt info:

  1. Select “Pipes” from the list on the left.
  2. In the list of pipes, click on the “New Pipe” button in the upper-right of the screen.
  3. In the new pipe, click on the “Execute” button to add an Execute step to the pipe tasks.
  4. In the new execute step, select the “Inline script” option, “Python” for the language, and then paste the following code snippet in the code window:
  1. Save the newly added execute step by clicking on the “Save” button in the upper-right of the builder.
  2. At the top of your script, make sure the script references your gmail connection using the “my-gmail” alias you gave your Gmail connection. If you gave your Gmail connection a different alias, then update the alias in your code and resave the pipe.
  3. Run your pipe by clicking on “Test” in the upper-right of the pipe builder.
  4. You’ll see a table with some basic information from the first part of your emails.

Save the receipt info to a spreadsheet

Once you’ve extracted the information you want from your email messages, the final step is to save the receipt info to a spreadsheet. Here’s how:

  1. In the pipe builder, click on the plus at the bottom-right of the execute step to add a new step.
  2. In the new step, select “Convert”.
  3. In the convert step, select “JSON” in the input format and “Table” in the output format
  4. Save the newly added convert step by clicking on the “Save” button in the upper-right of the builder.
  5. Run your pipe by clicking on “Test” in the upper-right of the pipe builder.

You should see your email info in a table with a download button.

Deployment

Because this pipe is run manually, you don’t have to do anything special to deploy it. You can simply run it each time you need the results, and then click the “download” button once you’ve run your script.