Laravel 10 How to change SMTP details on the fly without changing config values

53 views Asked by At

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

There are 2 answers

1
kawax On

Laravel10 using Symfony Mailer instead of SwiftMailer. https://laravel.com/docs/9.x/upgrade#symfony-mailer

config overrides are temporary and are not saved.

config(['mail.mailers.smtp.username' => '']);
config(['mail.mailers.smtp.password' => '']);

// sendmail

This only takes effect during the current request.

0
Mukesh Khatri On

I have changed some logic as mention @kawax "Laravel10 using Symfony Mailer instead of SwiftMailer", so checked Laravel core files and found "Illuminate\Mail\MailManager" 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.

    <?php

    namespace App\Providers;

    use Illuminate\Support\ServiceProvider;
    use Illuminate\Mail\Mailer;
    use Illuminate\Support\Arr;
    use Illuminate\Mail\MailManager;

    class AppServiceProvider extends ServiceProvider
    {
        /**
         * Register any application services.
         */
        public function register(): void
        {
            /* Set custom mailer to send mail tennat SMTP detail */
            $this->app->bind('user.mailer', function ($app, $parameters) {
                if ($parameters['smtp_transpot'] == 'smtp') {
                    // $from_email = Arr::get($parameters, 'smtp_from_email');
                    // $from_name = Arr::get($parameters, 'smtp_from_name');
                    $configSmtp = [
                        "transport" => $parameters['smtp_transpot'],
                        "host" => Arr::get($parameters, 'smtp_host'),
                        "port" => Arr::get($parameters, 'smtp_port'),
                        "encryption" => Arr::get($parameters, 'smtp_encryption'),
                        "username" => Arr::get($parameters, 'smtp_username'),
                        "password" => Arr::get($parameters, 'smtp_password'),
                        "timeout" => null,
                        "local_domain" => null
                    ];
                    $mailManager = new MailManager($app);
                    $smtpTransportFactory = $mailManager->createSymfonyTransport($configSmtp);

                    $mailer = new Mailer('smtp', $app->get('view'), $smtpTransportFactory, $app->get('events'));
                    // $mailer->alwaysFrom($from_email, $from_name);
                    // $mailer->alwaysReplyTo($from_email, $from_name);

                    return $mailer;
                }
            });
        }

        /**
         * Bootstrap any application services.
         */
        public function boot(): void
        {
            //
        }
    }

Send mail in controller (same as queue)

$configuration = $this->configuration();
$customer = Customer::find(1);
$notification['message'] = "Message";
$customerMailer = new CustomerMail($customer, $notification['message'], $configuration['smtp_from_email'], $configuration['smtp_from_name']);
// call here
$mailer = app()->makeWith('user.mailer', $configuration);
$mailer->to($customer->{Customer::EMAIL})->send($customerMailer);

This work for me.

Thank You!