Mailgun.Models.SignedEvent 1.1.1

Implements data model for incoming Mailgun events to use with your custom Mailgun webhooks.

This library can be used as a data model for the deserialization of this data with any JSON serializer of your choice. It even provides a handy function to verify its cryptographic signature.

Install-Package Mailgun.Models.SignedEvent -Version 1.1.1
dotnet add package Mailgun.Models.SignedEvent --version 1.1.1
<PackageReference Include="Mailgun.Models.SignedEvent" Version="1.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Mailgun.Models.SignedEvent --version 1.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Background

According to Mailgun they provide developer friendly transactional e-mail service. In spite of this claim there's still no official SDK for nearly any platform and their documentation often lacks basic information. However it's still one of the best options you have if you don't want to implement your own e-mail delivery service which can become very complex very quickly.

Once you have an account with them you can subscribe to various messaging events so when the appropriate event happens (eg. the e-mail was delivered or bounced) Mailgun will POST a JSON encoded object to the URL you provided.

This library was created to ease the burden of deserializing these events into POCOs and to provide an easy way to verify the cryptographic signature of an incoming packet.

Usage

Installation

Add this package as a dependency to your project using NuGet.

Example

Here's a practical example using ASP.NET Core 3.1:

using Mailgun.Models.SignedEvent;

...

[Route("[controller]")]
[ApiController]
public class DeliveredController : ControllerBase
{
    [HttpPost]
    public async Task<ActionResult<SignedEvent>> PostDelivered([FromBody] SignedEvent signedEvent)
    {
        // ASP.NET Core framework will automagically deserialize JSON into signedEvent (see notes at the bottom regarding caveats)
        if (!signedEvent.Signature.IsValid("your-api-key"))
        {
            // if the signature is invalid, return 401
            return Unauthorized();
        }

        // do something meaningful with signedEvent.Event here

        // finally return 201 so Mailgun knows POST has been successful. Otherwise it'll keep retrying
        return CreatedAtAction(nameof(PostDelivered), null);
    }
}

A SignedEvent contains a Signature and the actual Event. While not mandatory it's recommended to check the signature to make sure it was actually signed by Mailgun.

Since the signature is created using the signing server's own time you can specify how old a signature can be to still be considered as valid. By default this is set to 10 minutes.

Important Notes

Unusual JSON Naming Convention

Mailgun generates JSON data using an unusual naming convention with dashes between words. This means that the verb is valid will be encoded as is-valid even though the convention is to encode names as camelCase (resulting in isValid).

There are ways to configure most JSON serializers to handle this, but this topic is out of scope of this project.

Structure Is Constantly Changing

Please note that these events can apparently change their structure anytime, so don't be surprised if the data you've just received contains new fields.

If you discover a change (which can only be an addition according to their documentation) you're welcome to open a PR.

Background

According to Mailgun they provide developer friendly transactional e-mail service. In spite of this claim there's still no official SDK for nearly any platform and their documentation often lacks basic information. However it's still one of the best options you have if you don't want to implement your own e-mail delivery service which can become very complex very quickly.

Once you have an account with them you can subscribe to various messaging events so when the appropriate event happens (eg. the e-mail was delivered or bounced) Mailgun will POST a JSON encoded object to the URL you provided.

This library was created to ease the burden of deserializing these events into POCOs and to provide an easy way to verify the cryptographic signature of an incoming packet.

Usage

Installation

Add this package as a dependency to your project using NuGet.

Example

Here's a practical example using ASP.NET Core 3.1:

using Mailgun.Models.SignedEvent;

...

[Route("[controller]")]
[ApiController]
public class DeliveredController : ControllerBase
{
    [HttpPost]
    public async Task<ActionResult<SignedEvent>> PostDelivered([FromBody] SignedEvent signedEvent)
    {
        // ASP.NET Core framework will automagically deserialize JSON into signedEvent (see notes at the bottom regarding caveats)
        if (!signedEvent.Signature.IsValid("your-api-key"))
        {
            // if the signature is invalid, return 401
            return Unauthorized();
        }

        // do something meaningful with signedEvent.Event here

        // finally return 201 so Mailgun knows POST has been successful. Otherwise it'll keep retrying
        return CreatedAtAction(nameof(PostDelivered), null);
    }
}

A SignedEvent contains a Signature and the actual Event. While not mandatory it's recommended to check the signature to make sure it was actually signed by Mailgun.

Since the signature is created using the signing server's own time you can specify how old a signature can be to still be considered as valid. By default this is set to 10 minutes.

Important Notes

Unusual JSON Naming Convention

Mailgun generates JSON data using an unusual naming convention with dashes between words. This means that the verb is valid will be encoded as is-valid even though the convention is to encode names as camelCase (resulting in isValid).

There are ways to configure most JSON serializers to handle this, but this topic is out of scope of this project.

Structure Is Constantly Changing

Please note that these events can apparently change their structure anytime, so don't be surprised if the data you've just received contains new fields.

If you discover a change (which can only be an addition according to their documentation) you're welcome to open a PR.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Mailgun.Models.SignedEvent:

Package Downloads
Mailgun.Middleware.AuthenticationHandler
Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.1.1 44 6/29/2020
1.1.0 63 6/22/2020
1.0.2 99 6/22/2020
1.0.1 70 6/19/2020
1.0.0 83 6/18/2020
1.0.0-rc1 99 6/18/2020
1.0.0-rc0 44 6/18/2020