craigpaul / laravel-postmark

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

Multiple Mailers with Different Streams #140

Closed nacho-villanueva closed 1 year ago

nacho-villanueva commented 1 year ago

I'm trying to configure 2 mailers, one for postmark transactionals and another for broadcasts. in the following way, but when I try to send a notification using the "postmark-broadcast" it doesn't take use the message_stream_id of that mailer, instead uses the one from "postmark" mailer. Is this a bug, or am I doing something wrong?

'postmark' => [
  'transport' => 'postmark',
],
'postmark-broadcast' => [
  'transport' => 'postmark',
  'message_stream_id' => env('POSTMARK_BROADCAST_MESSAGE_STREAM', 'broadcast'),
],
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->mailer('postmark-broadcast')
            ->subject($this->company->name.": New Investor Update '".$this->investorUpdate->title."'")
            ->markdown('emails.new_investor_update', [
                'url' => route('investor-update', ['id' => $this->investorUpdate->hash, 'as' => $notifiable->routes['mail']]),
                'title' => $this->investorUpdate->title,
                'company' => $this->company->name,
                'creator' => $this->creator,
                'section_title' => $this->firstSection->title,
                'section_body' => $this->firstSection->text,
            ]);
    }
nacho-villanueva commented 1 year ago

In the PostmarkServiceProvider.php I found that the message_stream_id is always being fetched from the mailer named 'postmark', as you can see below.

public function boot(): void
    {
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'postmark');

        $this->app['mail.manager']->extend('postmark', function () {
            $config = $this->app->make('config');

            return new PostmarkTransport(
                $this->app->make(Factory::class),
                $config->get('mail.mailers.postmark.message_stream_id'),
                $config->get('services.postmark.token'),
            );
        });
    }

As a work around, I solved it be creating a new Transport in my AppServiceProvider, but probably it should be changed in the repo. Here is my change:

        Mail::extend('postmark-broadcast', function ($config) {
            $globalService = $this->app->make('config');
            return new PostmarkTransport(
                $this->app->make(Factory::class),
                $config['message_stream_id'],
                $globalService->get('services.postmark.token'),
            );
        });

Cheers!

craigpaul commented 1 year ago

Hey @nacho-villanueva! Great catch, thank you! This is resolved in v3.0.4 with 50147a4cb8