Use an Azure function to intercept your MailGun webhooks.
HandleWebhook
receives any webhook from MailGun, then invokes the appropriate response strategy based on theMailGunEvent
type. For example, it might add outgoing alert email messages to the 'email-outbox' storage queue.DrainOutbox
processes the 'email-outbox' queue, sending queued emails via the SendGrid API.
The request body payload is deserialized to C# classes to make working with the data easier and more type-safe.
- If MailGun is having issues, it might make sense to send the alerts via another channel
- SendGrid is easier to work with in Azure. There's a nuget package and everything. :)
- Clone the repo. :)
- Create
local.settings.json
in the root of the repo and set it up like this:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"alert-email-addresses": "{ This email addresses to alert, separated by semicolons ';' }",
"from-email-address": "{ Your email address }",
"mailgun-webhook-signing-key": "{ Get this from https://app.mailgun.com/app/account/security/api_keys }",
"sendgrid-api-key": "{ Your API key for SendGrid }"
}
}
- Run the project in Visual Studio (not Visual Studio Code if you want the full development experience)
- Run ngrok and point it against your visual studio dev server
- Head over to the mailgun webhooks page for your site, for example https://app.mailgun.com/app/sending/domains/example.com/webhooks. From there you can paste in your URL and see what happens. As coded, the URL might look something like https://deadbeef.ngrok.io/api/tygerbytes/mailgun/webhook.
- Create new implementations of
IResponseStrategy
to customize all the response things.
Have fun. 😊