Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using non-default mailers #16

Open
ejunker opened this issue May 24, 2024 · 0 comments
Open

Using non-default mailers #16

ejunker opened this issue May 24, 2024 · 0 comments

Comments

@ejunker
Copy link

ejunker commented May 24, 2024

I have multiple mailers configured in my config/mail.php with the default set to sendmail

    'default' => env('MAIL_MAILER', 'sendmail'),

    'mailers' => [
        'sendmail' => [
            'transport' => 'sendmail',
            'path' => env('MAIL_SENDMAIL_PATH', '/usr/sbin/sendmail -bs -i'),
        ],

        'sparkpost' => [
            'transport' => 'sparkpost',
        ],

If I have code that uses a specific mailer to send mail I do something like this in my test:

class MailableTest extends TestCase
{
    use WithMailInterceptor;

    public function test_example(): void
    {
        $this->interceptMail();
        Mail::mailer('sparkpost')->send(new MyMailable());
        $this->assertNotEmpty($this->interceptedMail());
    }
}

This test fails. I discovered if I change:

    public function interceptedMail(): Collection
    {
        return app('mailer')->getSymfonyTransport()
            ->messages()
            ->map(function (SentMessage $message) {
                return new AssertableMessage($message->getOriginalMessage());
            });
    }

to using the mail manager and allow specifying the mailer like this:

    public function interceptedMail(?string $mailer = null): Collection
    {
        return app('mail.manager')->mailer($mailer)->getSymfonyTransport()
            ->messages()
            ->map(function (SentMessage $message) {
                return new AssertableMessage($message->getOriginalMessage());
            });
    }

and then in my test do this it works:

$this->assertNotEmpty($this->interceptedMail('sparkpost'));

Is there a better way to handle this? This was just an example test; in the real world this would be a feature test where I am not sending the mailable directly in the test but rather making a request to my app that generates the email.

I was considering creating a PR with this change, but I wasn't sure how to add a test that covers this functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant