PHPackages                             binary-cats/laravel-mailgun-webhooks - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. binary-cats/laravel-mailgun-webhooks

ActiveLibrary[HTTP &amp; Networking](/categories/http)

binary-cats/laravel-mailgun-webhooks
====================================

Handle Mailgun webhooks in a Laravel application

9.6.0(1mo ago)64299.4k↓28.5%8[1 PRs](https://github.com/binary-cats/laravel-mailgun-webhooks/pulls)MITPHPPHP ^8.0

Since Dec 15Pushed 6mo ago3 watchersCompare

[ Source](https://github.com/binary-cats/laravel-mailgun-webhooks)[ Packagist](https://packagist.org/packages/binary-cats/laravel-mailgun-webhooks)[ Docs](https://github.com/binary-cats/laravel-mailgun-webhooks)[ RSS](/packages/binary-cats-laravel-mailgun-webhooks/feed)WikiDiscussions v9.x Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (29)Used By (0)

[![](https://camo.githubusercontent.com/2bedf63f24cda7efab02da955dc11fb7ef8a060e2f26b73c33a7aac84529b8a3/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f737570706f72742d756b7261696e652e7376673f743d31)](https://supportukrainenow.org)

Handle Mailgun Webhooks in a Laravel application
================================================

[](#handle-mailgun-webhooks-in-a-laravel-application)

[![https://github.com/binary-cats/laravel-mailgun-webhooks/actions](https://github.com/binary-cats/laravel-mailgun-webhooks/workflows/Laravel/badge.svg)](https://github.com/binary-cats/laravel-mailgun-webhooks/workflows/Laravel/badge.svg)[![https://github.styleci.io/repos/230519748](https://camo.githubusercontent.com/0a10bd0ccd3a2ad170cf960173177c19a61345e79584b105d1128fbabd7a1d21/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3233303531393734382f736869656c64)](https://camo.githubusercontent.com/0a10bd0ccd3a2ad170cf960173177c19a61345e79584b105d1128fbabd7a1d21/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3233303531393734382f736869656c64)[![https://scrutinizer-ci.com/g/binary-cats/laravel-mailgun-webhooks/](https://camo.githubusercontent.com/72fa749e73aa4c4788d23be9b34c16cb3da67e20bde93ec59f03bdce0086d37c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62696e6172792d636174732f6c61726176656c2d6d61696c67756e2d776562686f6f6b732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://camo.githubusercontent.com/72fa749e73aa4c4788d23be9b34c16cb3da67e20bde93ec59f03bdce0086d37c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62696e6172792d636174732f6c61726176656c2d6d61696c67756e2d776562686f6f6b732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)

[Mailgun](https://mailgun.com) can notify your application of mail events using webhooks. This package can help you handle those webhooks. Out of the box it will verify the Mailgun signature of all incoming requests. All valid calls will be logged to the database. You can easily define jobs or events that should be dispatched when specific events hit your app.

This package will not handle what should be done after the webhook request has been validated and the right job or event is called. You should still code up any work (eg. what should happen) yourself.

[![](https://repository-images.githubusercontent.com/230519748/7af61180-28c0-11ea-9377-a471da1dbb32)](https://repository-images.githubusercontent.com/230519748/7af61180-28c0-11ea-9377-a471da1dbb32)

Before using this package we highly recommend reading [the entire documentation on webhooks over at Mailgun](https://documentation.mailgun.com/en/latest/api-webhooks.html).

This package is an adapted copy of absolutely amazing [spatie/laravel-stripe-webhooks](https://github.com/spatie/laravel-stripe-webhooks)

Upgrade
-------

[](#upgrade)

If you are upgrading from previous version, please note that spatie/laravel-webhook-client has been upgraded to ^3.0 - which adds an extra field into the webhooks table. Read [upgrading instructions](https://github.com/spatie/laravel-webhook-client/blob/main/UPGRADING.md) for more details.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require binary-cats/laravel-mailgun-webhooks
```

The service provider will automatically register itself.

You must publish the config file with:

```
php artisan vendor:publish --provider="BinaryCats\MailgunWebhooks\MailgunWebhooksServiceProvider" --tag="config"
```

This is the contents of the config file that will be published at `config/mailgun-webhooks.php`:

```
return [

    /*
     * Mailgun will sign each webhook using a secret. You can find the used secret at the
     * webhook configuration settings: https://app.mailgun.com/app/account/security/api_keys.
     */
    'signing_secret' => env('MAILGUN_WEBHOOK_SECRET'),

    /*
     * You can define the job that should be run when a certain webhook hits your application
     * here. The key is the name of the Mailgun event type with the `.` replaced by a `_`.
     *
     * You can find a list of Mailgun webhook types here:
     * https://documentation.mailgun.com/en/latest/api-webhooks.html#webhooks.
     *
     * The package will automatically convert the keys to lowercase, but you should
     * be congnisant of the fact that array keys are case sensitive
     */
    'jobs' => [
        // 'delivered' => \BinaryCats\MailgunWebhooks\Jobs\HandleDelivered::class,
    ],

    /*
     * The classname of the model to be used. The class should equal or extend
     * Spatie\WebhookClient\Models\WebhookCall
     */
    'model' => \Spatie\WebhookClient\Models\WebhookCall::class,

    /*
     * The classname of the model to be used. The class should equal or extend
     * BinaryCats\MailgunWebhooks\ProcessMailgunWebhookJob
     */
    'process_webhook_job' => \BinaryCats\MailgunWebhooks\ProcessMailgunWebhookJob::class,
];
```

In the `signing_secret` key of the config file you should add a valid webhook secret. You can find the secret used at [HTTP webhook signing key](https://app.mailgun.com/app/account/security/api_keys).

**You can skip migrating is you have already installed `Spatie\WebhookClient`**

Next, you must publish the migration with:

```
php artisan vendor:publish --provider="Spatie\WebhookClient\WebhookClientServiceProvider" --tag="webhook-client-migrations"
```

After migration has been published you can create the `webhook_calls` table by running the migrations:

```
php artisan migrate
```

### Routing

[](#routing)

Finally, take care of the routing: At [the Mailgun dashboard](https://app.mailgun.com/app/sending/domains) you must configure at what url Mailgun webhooks should hit your app. In the routes file of your app you must pass that route to `Route::mailgunWebhooks()`:

I like to group functionality by domain, so I would suggest `webhooks/mailgun` (especially if you plan to have more webhooks), but it is up to you.

```
# routes\web.php
Route::mailgunWebhooks('webhooks/mailgun');
```

Behind the scenes this will register a `POST` route to a controller provided by this package. Because Mailgun has no way of getting a csrf-token, you must add that route to the `except` array of the `VerifyCsrfToken` middleware:

```
protected $except = [
    'webhooks/mailgun',
];
```

Usage
-----

[](#usage)

Mailgun will send out webhooks for several event types. You can find the [full list of events types](https://documentation.mailgun.com/en/latest/user_manual.html#events) in Mailgun documentation.

Mailgun will sign all requests hitting the webhook url of your app. This package will automatically verify if the signature is valid. If it is not, the request was probably not sent by Mailgun.

Unless something goes terribly wrong, this package will always respond with a `200` to webhook requests. Sending a `200` will prevent Mailgun from resending the same event over and over again. All webhook requests with a valid signature will be logged in the `webhook_calls` table. The table has a `payload` column where the entire payload of the incoming webhook is saved.

If the signature is not valid, the request will not be logged in the `webhook_calls` table but a `BinaryCats\MailgunWebhooks\Exceptions\WebhookFailed` exception will be thrown. If something goes wrong during the webhook request the thrown exception will be saved in the `exception` column. In that case the controller will send a `500` instead of `200`.

There are two ways this package enables you to handle webhook requests: you can opt to queue a job or listen to the events the package will fire.

**Due to the apparent differences between MailGun sandbox and production environment event casing, the package will ALWAYS cast mailgun events to lowercase - so your configured keys must be lowercase, too**

**The package does not handle legacy webhooks, as they have a different schema.** Let me know if this is something that is needed.

### Handling webhook requests using jobs

[](#handling-webhook-requests-using-jobs)

If you want to do something when a specific event type comes in you can define a job that does the work. Here's an example of such a job:

```
