skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.

        <?php
    
        namespace AppProviders;
    
        use IlluminateSupportServiceProvider;
        use IlluminateMailMailer;
        use IlluminateSupportArr;
        use IlluminateMailMailManager;
    
        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!


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search