I’m trying to change SMTP details on the fly for web or queue also.
I have tried this link : https://laravel-news.com/allowing-users-to-send-email-with-their-own-smtp-settings-in-laravel, but this example not working in Laravel 10.
Not only that, but I also try this code :
$configuration = $this->configuration();
$customer = Customer::find(1);
$notification['message'] = "Message";
$mailer = new CustomerMail($customer, $notification['message'], $configuration['smtp_from_email'], $configuration['smtp_from_name']);
$mailer->withSwiftMessage(function ($message) use ($configuration) {
$message->getHeaders()
->addTextHeader('X-SMTP-Transport', $configuration['smtp_transpot'])
->addTextHeader('X-SMTP-Host', $configuration['smtp_host'])
->addTextHeader('X-SMTP-Port', $configuration['smtp_port'])
->addTextHeader('X-SMTP-Encryption', $configuration['smtp_encryption'])
->addTextHeader('X-SMTP-Username', $configuration['smtp_username'])
->addTextHeader('X-SMTP-Password', $configuration['smtp_password']);
});
Mail::to($customer->{Customer::EMAIL})->send($mailer);
This code also not working for me, How can I change SMTP details on the fly in Laravel 10 without effecting config values.
2
Answers
I have changed some logic as mention @kawax "Laravel10 using Symfony Mailer instead of SwiftMailer", so checked Laravel core files and found "IlluminateMailMailManager" class handle configuration of Symfony Transport with "createSymfonyTransport()" function.
I am still using Laravel News blog : https://laravel-news.com/allowing-users-to-send-email-with-their-own-smtp-settings-in-laravel, but Change some logic in AppServiceProvider class for Laravel 10.
Send mail in controller (same as queue)
This work for me.
Thank You!
Laravel10 using Symfony Mailer instead of SwiftMailer.
https://laravel.com/docs/9.x/upgrade#symfony-mailer
config overrides are temporary and are not saved.
This only takes effect during the current request.