craigpaul / laravel-postmark

A Postmark adapter for Laravel
MIT License
204 stars 23 forks source link

catch postmark message id in an event listener when message is sent #146

Closed xiaoCreagia closed 7 months ago

xiaoCreagia commented 7 months ago

I'm using this package to send my emails via Postmark (it already works), but I need to store the postmark message ID into DB. I have an event listener like this, but headers only contains "from","to" and "subject"...

public function handle(MessageSent $event) { if (isset($event->data['booking'])) { $booking = $event->data['booking']; $messageId = $event->message->getHeaders()->get('x-pm-message-id')?->getValue() ?? $event->message->getHeaders()->get('message-id')->getName(); $booking->mailIdentifiers()->create(['message_identifier' => $messageId]); } }

On my postmarks' account looks like this: image

craigpaul commented 7 months ago

Hey @xiaoCreagia, in order to get the message ID from Laravel you will need to add a listener for the \Illuminate\Mail\Events\MessageSent event in the EventServiceProvider. Eg.

<?php

namespace App\Providers;

use App\Listeners\HandleMessageSent;
use Illuminate\Mail\Events\MessageSent;

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        MessageSent::class => [
            HandleMessageSent::class,
        ],
    ];
}
<?php

namespace App\Listeners;

use Illuminate\Mail\Events\MessageSent;

class HandleMessageSent
{
    public function handle(MessageSent $message)
    {
        $messageId = $event->sent->getSymfonySentMessage()->getMessageId();

        // ...
    }
}