Download the PHP package aleahy/mailgun-logger without Composer
On this page you can find all versions of the php package aleahy/mailgun-logger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aleahy/mailgun-logger
More information about aleahy/mailgun-logger
Files in aleahy/mailgun-logger
Package mailgun-logger
Short Description A package to receive webhooks from mailgun and log the events
License MIT
Informations about the package mailgun-logger
MailgunLogger
This package allows a laravel app to receive Mailgun webhooks. It makes use of Spatie's Webhook Client Package to capture the webhooks. You then can queue jobs to process the webhooks as you see fit.
It also has a service to retrieve stored messages through the api.
Installation
Install the package using composer:
The service provider should autoregister.
You will need your mailgun signing secret in your .env
file.
If you want to define jobs to respond to the webhooks, you will need to publish the config file.
Publish the config file using
You can then define your jobs in the jobs section of the mailgun-logger
config file.
Make sure the key for each job you define is one of the event types defined in the Mailgun API.
You will need the migrations from the Spatie\WebhookClient Package to store the webhook calls.
If you haven't already done so, publish them using:
and run the migration:
Routing
Define a route for incoming mailgun events in your routes/web.php
file using the Route macro mailgunWebhooks
:
This will register a POST
route to a controller in this package.
You will also need to bypass CSRF token checking for this route by adding it
to the VerifyCsrfToken
middleware.
Usage
Responding to Webhooks
Create a laravel job class that extends Spatie\WebhookClient\Jobs\ProcessWebhookJob
to be run when the webhook is received.
As the parent class receives the webhook in the constructor,
the payload can be access by $this->webhookCall->payload
.
If you decide not to extend Spatie\WebhookClient\Jobs\ProcessWebhookJob
,
then make sure it can receive the webhook in the constructor.
It is also important that the job is queueable so that Mailgun can receive a response immediately.
An example is shown below:
Once the job class is created, just add it to the jobs
array in the mailgun-logger
config file.
The key is the event type, as listed in the Mailgun API docs, and the value is the job class name.
Getting Stored Messages
Some of the mailgun webhooks contain a url to the stored message.
You can access the contents of the message using the MailgunService
class.
This will return an associative array containing various forms of the message,
under keys such as stripped-text
, body-html
, stripped-html
.
You can also get the raw MIME message.
This also returns an associative array which contains the mime data under the key body-mime
.
Upgrading from 0.3 to 1
Version 1.0 requires PHP 8.0 and also has upgraded Spatie\WebhookClient
to version 3.1.7.\
The migrations for this package has changed, so in order to upgrade, you must add the following migration:
Credits
Much of this package was influenced by binary-cats/laravel-mailgun-webhooks.
Thanks also to Spatie for creating such amazing packages, including their Webhook Client Package which this package makes use of.
All versions of mailgun-logger with dependencies
spatie/laravel-webhook-client Version ^3.1.7
guzzlehttp/guzzle Version ^7.3