Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aescaru's avatar

Using Mail::mailer() still sending from default mailer

We recently started up a new project using laravel 9. For a specific controller we want to send mails from a different mail config than specified in .env file. Per docs this should be as easy as using Mail::mailer('custommailconfig')->to('[email protected]')->send(new SampleMail());

We added the new config to the config/mail.php to the mailers array 'custommailconfig' => [ 'transport' => 'smtp', 'host' => 'host', 'port' => port, 'encryption' => 'ssl', 'username' => 'username', 'password' => 'password', 'timeout' => null, 'local_domain' => env('MAIL_EHLO_DOMAIN') ]

however, the default mailer is still used alongside the credentials set in .env. I did php artisan config:clear and cache:clear and restarted the server

0 likes
12 replies
Sinnbeck's avatar

So that new config isnt reading from the .env or ? It seems all values are hardcoded

aescaru's avatar

@Sinnbeck if i overwrite the settings in .env it works. But that isnt really what I want. I want a default mailer which is specified in .env but specify and use a different host when needed wihtout needing to change the .env.

Sinnbeck's avatar

@aescaru Yes I get that but that wasnt the question. In your config, how the custommailconfig getting its settings? Can you show the actual config, and not just dummy data?

aescaru's avatar

@Sinnbeck i dont really know what you mean but I added the

'custommailconfig' => [
            'transport' => 'smtp',
            'host' => '***.de',
            'port' => 465,
            'encryption' => 'ssl',
            'username' => '***@***.de',
            'password' => '******',
            'timeout' => null
        ]

to the mailers array in mail.php. This data is hardcoded, yes. for reference, the preconfigured smtp one:

'smtp' => [
            'transport' => 'smtp',
            'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
            'port' => env('MAIL_PORT', 587),
            'encryption' => env('MAIL_ENCRYPTION', 'tls'),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => null,
            'local_domain' => env('MAIL_EHLO_DOMAIN')
        ],
Sinnbeck's avatar

@aescaru Can you please format your code by adding ``` on the line before and after it?

aescaru's avatar

ohhhh wait... I guess I got thrown off by the mail address that is still set under the Global "From" Address....

aescaru's avatar

@Sinnbeck did that. guess i am thrown off by the config that is set in 'from' in mail.php. Definitly was that.

Sinnbeck's avatar

I just tested it myself, with the log mailer

Mail::mailer('log')->to('[email protected]')->send(new SampleMail());

And the mail showed up in laravel.log. Does this work for you ?

aescaru's avatar

@Sinnbeck Found the issue, After inspecting the mails it worked correctly. The global set from address got me confused here

newbie360's avatar

@aescaru actually each host is use different .env , no ? otherwise on each runtime, has some logic to get the mailer base on the host... hmm no comment ;)

Sinnbeck's avatar

Please mark a post as best answer to set the thread as solved :)

Please or to participate in this conversation.