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

Dreamer's avatar

How to manually provide smtp information for Mailable

I am creating an app where users can save their smtp information and can then send emails through the app to their clients. Can i use Mailable class for it or what should i do to make it happen. Right now, Laravel uses global settings for all emails.

0 likes
7 replies
bobbybouwmann's avatar

You can set the config on runtime.

Config::set('mail.port', 587); // default

You can simply replace the values with values from the database ;)

Note: Configuration values that are set at run-time are only set for the current request, and will not be carried over to subsequent requests.

Note that this can give issues when users give in incorrect details and the mails won't send

1 like
Dreamer's avatar

@bobbybouwmann Thank you, this gets me a step closer! One thing though... If the email is queued, how does it work then? Does it use smtp settings that were present during the time mail was sent to queue or smtp settings that are present when the mail is actually sent?

At least now i can send emails...

Dreamer's avatar

One solution that i can think of is to create an Email model (i do need detailed email log anyway) and save all data needed to send an email including smtp settings from user to database. Then, i can make a queued job that gets email id and retrieves Email model object from database and just calls send method on it. That send method on Email model news mailable class, sets all needed attributes and uses Config::set() to update smtp information and then sends it.

Will this work?

harran's avatar
harran
Best Answer
Level 1

you can add a private method in your Mailable class to switch the settings like this:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class TestEmail extends Mailable
{
    use Queueable, SerializesModels;

    public $data;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($data, $smtpSettings)
    {
        $this->data = $data;
        $this->switchMailSettings($smtpSettings);
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {   

        return $this->view('emails.test');
    }


    private function switchMailSettings($smtpSettings){

        config()->set([
                    'mail.host' =>  $smtpSettings['smtp_host'],
                    'mail.port' =>  $smtpSettings['smtp_port'],
                    'mail.encryption' => $smtpSettings['smtp_secure'],
                    'mail.username' => $smtpSettings['from_email'],
                    'mail.password' => $smtpSettings['from_email_password'],
                    'mail.from' => [
                        'address' => $smtpSettings['from_email'], 
                        'name' =>  $smtpSettings['from_name'],
                    ],
                ]);
    }

}
Dreamer's avatar

@harran So, what you are saying is that if i set smtp config values inside a queue (inside mailable clas or job), the email will be sent using changed settings...

If that is true then now i have all the information i need to code it :) Not sure yet if i set config inside Mailable or Email model.

1 like
bobbybouwmann's avatar

If you want to queue your emails you should change the settings in your Mailable class, otherwise it will use the default config.

When you use Config::set() it will only set it for that request, so when you queue the mail it will be in a new request.

1 like

Please or to participate in this conversation.