mvdnbrk / laravel-postmark-webhooks
Handle Postmark webhooks in a Laravel application.
Package info
github.com/mvdnbrk/laravel-postmark-webhooks
pkg:composer/mvdnbrk/laravel-postmark-webhooks
Fund package maintenance!
Requires
- php: ^8.2
- illuminate/database: ^12.0 || ^13.0
- illuminate/http: ^12.0 || ^13.0
- illuminate/routing: ^12.0 || ^13.0
- illuminate/support: ^12.0 || ^13.0
Requires (Dev)
- orchestra/testbench: ^10.0 || ^11.0
- phpunit/phpunit: ^11.0 || ^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Handle Postmark webhooks in a Laravel application
Postmark can send out several webhooks to your application when an event occurs.
This way Postmark is able to immediately notify you when something new occurs.
This package can help you handle those webhooks.
Requirements
- PHP 8.2 or higher
- Laravel 12 or 13 (Laravel 13 requires PHP 8.3 or higher)
Installation
You can install the package via composer:
composer require mvdnbrk/laravel-postmark-webhooks
This package will log all incoming webhooks to the database by default.
Run the migrations to create a postmark_webhook_logs table in the database:
php artisan migrate
If you want to disable database logging you can set
POSTMARK_WEBHOOKS_LOG_ENABLED=falsein your.envfile.
Setup webhooks with Postmark
Visit the servers page on your Postmark account.
Choose the server you want to setup webhooks for.
Go to settings > webhooks > add webbook.
This package will register a route where Postmark can post their webhooks to: /api/webhooks/postmark.
Fill in your webhook URL: https://<your-domain.com>/api/webhooks/postmark
Pick the events Postmark should send to you and save the webhook.
You are ready to receive webhook notifications from Postmark!
You may change the
/api/webhooks/postmarkendpoint to anything you like.
You can do this by changing thepathkey in theconfig/postmark-webhooks.phpfile.
The route uses the
apimiddleware group. You may change this with themiddlewarekey in the same file.
The check on Postmark's IP addresses is always added.
Protection of your webhook
This package protects your webhook automatically by only allowing requests from the IP range that Postmark uses.
When your app runs behind a proxy or load balancer (Cloudflare, ngrok, a load balancer), Laravel sees the proxy's IP address instead of Postmark's, and every webhook is rejected with a 401. Configure trusted proxies so Laravel reads the real client IP, for example in bootstrap/app.php:
->withMiddleware(function (Middleware $middleware) { $middleware->trustProxies(at: '*'); })
Only trust all proxies (*) when your app can't be reached except through the proxy.
The IP check is skipped when APP_ENV is local, so you can test webhooks with a tunnel such as ngrok.
Usage
Postmark can send out several event types by posting a webhook.
You can find the full list of webhooks in the Postmark documentation.
All webhook requests will be logged in the postmark_webhook_logs table.
The table has a payload column where the entire payload of the incoming webhook is saved.
The ID Postmark assigned to the original message will be saved in the message_id column,
the event type will be stored in the record_type column and the email address as well in the email column.
Note that event types will be converted to
snake_case.
For exampleSpamComplaintwill be saved asspam_complaint.
Events
Whenever a webhook call comes in, this package will fire a PostmarkWebhookCalled event.
Laravel discovers listeners in app/Listeners automatically by the event type-hint of their handle method.
You may also register a listener yourself in the boot method of your AppServiceProvider:
use Illuminate\Support\Facades\Event; use Mvdnbrk\PostmarkWebhooks\Events\PostmarkWebhookCalled; Event::listen(PostmarkWebhookCalled::class, YourListener::class);
Example of a listener:
<?php namespace App\Listeners; use Mvdnbrk\PostmarkWebhooks\Events\PostmarkWebhookCalled; class YourListener { public function handle(PostmarkWebhookCalled $event): void { // Do your work here. // You can access the payload here with: $event->payload. // The email address, message ID and record type are also available: // $event->email // $event->messageId // $event->recordType } }
You may also register an event listener for a specific type of event:
Event::listen('webhook.postmark: spam_complaint', YourSpamComplaintListener::class);
The listener receives the PostmarkWebhookCalled event as well.
A listener in
app/Listenerswith aPostmarkWebhookCalledtype-hint is also discovered for every webhook.
Keep listeners for a specific type outsideapp/Listenersto avoid running them twice.
Available events: open, bounce, click, delivery, spam_complaint, subscription_change.
Advanced configuration
You may optionally publish the config file with:
php artisan vendor:publish --provider="Mvdnbrk\PostmarkWebhooks\PostmarkWebhooksServiceProvider" --tag="config"
Within the configuration file you may change the table name being used or the Eloquent model being used to save log records to the database.
If you want to use your own model to save the logs to the database you should extend the
Mvdnbrk\PostmarkWebhooks\PostmarkWebhookclass.
You can also exclude one or more event types from being logged to the database.
Place the events you want to exclude under the except key:
'log' => [ ... 'except' => [ 'open', ... ], ],
You can map the events fired by this package to your own event classes:
'events' => [ 'spam_complaint' => App\Events\SpamComplaint::class, ... ],
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
Inspired by Laravel Stripe Webhooks from Spatie.
License
The MIT License (MIT). Please see License File for more information.