PHP code example of binary-cats / laravel-mailgun-webhooks

1. Go to this page and download the library: Download binary-cats/laravel-mailgun-webhooks library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

binary-cats / laravel-mailgun-webhooks example snippets


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,
];

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

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



namespace App\Jobs\MailgunWebhooks;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\WebhookClient\Models\WebhookCall;

class HandleDelivered implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /** @var \Spatie\WebhookClient\Models\WebhookCall */
    public $webhookCall;

    public function __construct(WebhookCall $webhookCall)
    {
        $this->webhookCall = $webhookCall;
    }

    public function handle()
    {
        // do your work here

        // you can access the payload of the webhook call with `$this->webhookCall->payload`
    }
}

// config/mailgun-webhooks.php

'jobs' => [
    'delivered' => \App\Jobs\MailgunWebhooks\HandleDelievered::class,
],

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'mailgun-webhooks::delivered' => [
        App\Listeners\DeliveredSource::class,
    ],
];



namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\WebhookClient\Models\WebhookCall;

class DeliveredSource implements ShouldQueue
{
    public function handle(WebhookCall $webhookCall)
    {
        // do your work here

        // you can access the payload of the webhook call with `$webhookCall->payload`
    }
}

use Spatie\WebhookClient\Models\WebhookCall;
use BinaryCats\MailgunWebhooks\ProcessMailgunWebhookJob;

dispatch(new ProcessMailgunWebhookJob(WebhookCall::find($id)));

use BinaryCats\MailgunWebhooks\ProcessMailgunWebhookJob;

class MyCustomMailgunWebhookJob extends ProcessMailgunWebhookJob
{
    public function handle()
    {
        // do some custom stuff beforehand

        parent::handle();

        // do some custom stuff afterwards
    }
}

Route::mailgunWebhooks('webhooks/mailgun/{configKey}');

Route::post('webhooks/mailgun/{configKey}', 'BinaryCats\MailgunWebhooks\MailgunWebhooksController');

// secret for when Mailgun posts to webhooks/mailgun/account
'signing_secret_account' => 'whsec_abc',
// secret for when Mailgun posts to webhooks/mailgun/my-named-secret
'signing_secret_my-named-secret' => 'whsec_123',
bash
php artisan vendor:publish --provider="BinaryCats\MailgunWebhooks\MailgunWebhooksServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="Spatie\WebhookClient\WebhookClientServiceProvider" --tag="webhook-client-migrations"
bash
php artisan migrate