riyas's avatar

Dynamically load configuration variable for email

Hello,

I am planning to use partner specific configuration for sending email from Lumen. Thus host, port, username, password etc will change from partner to partner

.env file these configurations are set to null as below

MAIL_DRIVER=mail
MAIL_HOST=
MAIL_PORT=
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_FROM_ADDRESS=
MAIL_FROM_NAME=
MAIL_ENCRYPTION=null

In the controller file, these values are updating values as below

config(['mail.host' => trim($data_list[0]->mailServerHostName),
                'mail.port' =>  trim($data_list[0]->mailServerPortNumber),
                'mail.username' =>  trim($data_list[0]->mailServerUsername),
                'mail.password' =>  trim($data_list[0]->mailServerPassword),            
                'mail.from.address' =>  trim($data_list[0]->emailFromAlias),
                'mail.from.name' => trim($data_list[0]->emailFromName) ]);

The values are updating properly. also print_r(config('mail')); will print the values. But while sending email, it is not taking the updated values...instead, it is taking the values from .env file.

Any help is appreciated

Thanks

Riyas

0 likes
1 reply
riyas's avatar
riyas
OP
Best Answer
Level 1

This issue is because, the new values for 'mail.from.address' and 'mail.from.name' are not updating properly. print_r(config('mail')) displays updated value in controller, but it is not taking the updated values while sending email. May be need to pass these two parameters in a different way

We manged it with following code

Code in Library file

config(['mail.host' => trim($data_list[0]->mailServerHostName),
        'mail.port' =>  trim($data_list[0]->mailServerPortNumber),
        'mail.username' =>  trim($data_list[0]->mailServerUsername),
        'mail.password' =>  trim($data_list[0]->mailServerPassword) ]);
</code>

FromEmail and FromName are moved to controller file

Mail::send('emails.welcome', $data, function($message)
{
    $message->from($data['fromEmail  '], $data['fromName']);
    $message->to( $data['toEmail'] )->subject($data['subject']);
   
});

Please or to participate in this conversation.