SendGrid will let you know, in near real-time, when emails fail to reach your customers. But what do you do after you have that information? Do you have a plan?
With this simple app, you can automatically create follow-up actions in Microsoft OneNote, right as email failures occur.
In this blog post, I show you how to use Flask (A Python Microframework), hosted on Microsoft Azure, to capture messages regarding email failures sent from the SendGrid Event Webhook. We then create OneNote pages, that contain follow-up actions, based on the particular email failure.
This code was developed using Microsoft Visual Studio Express 2013 for Web within VMWare Fusion 6.0.4, running Microsoft Windows Technical Preview on top of Mac OSX Yosemite. You will need SendGrid, Microsoft Azure, and OneNote accounts.
Get Python/Flask Running on Microsoft Azure
We will use CodePlex’s Python Tools for Visual Studio to simplify the process of quickly getting our Flask app up and running on Microsoft Azure. I prefer to live on the edge, so I’m using version 2.1 RC2, downloadable here.
Now, fire up Visual Studio and create a new app from a Blank Flask Web Project template:
Update your requirements.txt then right click on the project name:
Add your Python environment:
Log in to your Microsoft Azure management console and create a simple web site:
Download the publish settings:
Import the publish setting into your project by right clicking the project name, then select Publish:
Import the downloaded publish settings:
Before deploying the app, we need to update the credentials as environment variables. In your Azure management portal, click configure and scroll down to “app settings”:
Your SendGrid credentials can be found here, and to get your Microsoft OneNote token, create a client ID and then open this URL in a browser: https://login.live.com/oauth20_authorize.srf?client_id=[client_id]&scope=office.onenote_create&response_type=token where you will replace [client_id] to obtain your token.
Of course, that token only lasts for one hour, so for a production app, you’ll want to include the sign-up flow into your app’s UI, and then pass the token through in code. You can also get a standard OAUTH 2.0 refresh token so the sign-in can be persisted for up to a year. More details on authentication can be found here.
Set Up SendGrid’s Event Webhook and Send Bad Email Events to OneNote for Follow-Up
Log in to your SendGrid account, navigate to the App section and enter the Event Notification settings:
Add your URL to the HTTP Post URL field. For example, in my case it would be: http://sendgrid-onenote.azurewebsites.net/event. After you test your integration, you should see a notice similar to this:
Open up Microsoft OneNote and you should have a newly created page similar to this:
Finally, open your email client and you should have received an email:
Extending the Code
Check out the Event Webhook documentation to discover all of the event types you can watch for. Then head over to the OneNote API documentation to learn how to structure your Microsoft OneNote HTML message.
One interesting extension to this app is to pull contact information from the FullContact API to include in the Microsoft OneNote message.