We send most email from our Laravel applications through Postmark. Postmark then tells the application what happened to each message. It sends webhooks for deliveries, bounces, opens, clicks and spam complaints. Each application needs a route, authentication and code that reads the payload.
We wrote this code in many projects. Now it is a package: Laravel Postmark.
What the package does
Laravel Postmark receives the Postmark webhooks and changes each one into a Laravel event. You then use a usual listener for each type of event.
- It gives a route macro that adds the webhook endpoint.
- It checks each request against the Basic Auth credentials of the webhook. If you do not set the credentials, the endpoint refuses all requests.
- It replies to Postmark immediately. A queued job then dispatches the event.
- It gives typed methods for the payload, for example
isHardBounce()andoriginalLink(). - It gives the metadata that you attached to the message.
- It finds the addresses in a Postmark "inactive recipient" rejection.
Install the package
composer require bernskiold/laravel-postmark
Set the Basic Auth credentials of the webhook in your environment. Use the same credentials in Postmark.
POSTMARK_WEBHOOK_USERNAME=postmark
POSTMARK_WEBHOOK_PASSWORD=a-long-random-string
Add the route
Postmark sends requests from outside your application. Thus CSRF verification must not apply to the route. Put the route in your api routes:
// routes/api.php
Route::postmarkWebhooks('webhooks/postmark');
In Postmark, open the webhook settings of your server. Add a webhook with the full URL of the route, and enter the same credentials. Then select the events that you want to receive.
Listen for the events
The package dispatches one event for each type of webhook: EmailBounced, EmailDelivered, EmailOpened, EmailClicked, EmailComplained and EmailSubscriptionChanged.
For example, this listener marks an address as undeliverable after a hard bounce:
use Bernskiold\LaravelPostmark\Events\EmailBounced;
class FlagBouncedAddress
{
public function handle(EmailBounced $event): void
{
if ($event->isHardBounce()) {
Subscriber::query()
->where('email', $event->recipient())
->update(['undeliverable' => true]);
}
}
}
Postmark puts the address in Recipient on some webhooks and in Email on other webhooks. The recipient() method reads the correct field. The timestamp() method gives the time of the event as a Carbon date.
Find the message that caused an event
Attach metadata when you send the message. Postmark then includes the metadata in each event for that message.
return (new MailMessage)
->subject('Your invoice')
->tag('invoice')
->metadata('invoice_id', $this->invoice->id);
In the listener, read the metadata to find the record:
public function handle(EmailDelivered $event): void
{
Invoice::find($event->metadataValue('invoice_id'))?->markAsDelivered($event->timestamp());
}
Inactive recipients
Postmark makes an address inactive after a hard bounce, a spam complaint or a manual suppression. Postmark does not send to an inactive address, and the send operation throws an exception.
Only the text of the exception identifies this rejection. The InactiveRecipients class examines the text for you:
use Bernskiold\LaravelPostmark\Support\InactiveRecipients;
try {
$user->notify(new InvoiceReady($invoice));
} catch (Throwable $e) {
if (! InactiveRecipients::matches($e)) {
throw $e;
}
InactiveRecipients::extract($e)->each(
fn (string $email) => Subscriber::query()->where('email', $email)->update(['undeliverable' => true])
);
}
A limit to know
The package does not process the inbound webhook, because that webhook has a different payload. If a webhook has a missing or unknown record type, the job fails. The package writes the full payload to the log, so you can see the problem in your failed jobs.
Get the package
Laravel Postmark is open source, with the MIT license. The code and the full documentation are on GitHub. If you find a problem or have an idea, open an issue. We are happy to hear from you.