One of the questions I get asked a lot is how to get information from incoming emails entered into a Google Spreadsheet. In this post, I’ll show you one implementation on how to do this with the gspread module in Python.
Getting Started with Our Parse Webhook
The first thing we need to do is tell your domain that SendGrid will handle the email on its behalf. Then we tell SendGrid where to post the data from the email:
Modify MX Records
Change the MX Records of your domain to point to mx.sendgrid.net. This lets the registrar know that SendGrid will accept email messages on behalf of that domain.
Add Webhook URL
Once the email flows through us, we will parse out the components (https://sendgrid.com/docs/API_Reference/Webhooks/parse.html) of the email and post it a URL of your choice.
My domain in this case is Burrito.io, and I’m pointing to a URL where my app is located. You can set the address of your application here: https://sendgrid.com/developer/reply. Make sure to check incoming email for spam so that we can get the spam score posted to our application.
Setting up the GSpread Module and Your Google Spreadsheet
Install the gspread module (https://github.com/burnash/gspread). You can just pip install gspread or take a look at the other install options at the repo.
Next, make sure to create a Google Spreadsheet you want your data to be sent to.
Modify and Run the Code
Here is the code I created to get the From, Subject, and Spam Score of any email that goes to IEat@burrito.io. I created a quick Flask app with some functions to move around the spreadsheet.
Make sure to change the values of your Google login and spreadsheet name. Host this on your server and make sure to tell SendGrid to point here: