Reputation: 155
For each user, I change the email configuration in laravel project, when user login i use this:
\Config::set(['mail.mailers.smtp.host' => $user->smtp_mail_host]);
\Config::set(['mail.mailers.smtp.port' => $user->smtp_mail_port]);
\Config::set(['mail.mailers.smtp.username' => $user->smtp_mail_username]);
\Config::set(['mail.mailers.smtp.password' => $user->smtp_mail_password]);
\Config::set(['mail.from.address' => $user->mail_address]);
\Config::set(['mail.from.name' => $user->mail_from_name]);
when I did config()
it is showing my change, but when I send, the mail is sent from the .env configuration mail address, there is a missing part I'm not finding it.
thanks for help
Upvotes: 1
Views: 1233
Reputation: 417
if you using Queue and Jobs make sure to
php artisan queue:work
first (its seems to have it own cache where it store the configs)queue:work
already stop (for linux you can use top -c -p $(pgrep -d',' -f php)
and search for php artisan queue:work
in COMMAND column )config:clear
php artisan queue:work
Upvotes: 0
Reputation: 446
This is not optimal solution but can help! Pass the Configuration to your Mail::send before call send method such like this:
First import these classes in your Controller
use Mail;
use \Swift_Mailer;
use \Swift_SmtpTransport as SmtpTransport;
Then in your send function:
$from_email = \Config::get('mail.from.address');
$email_name = \Config::get('mail.from.name');
$to_email = "[email protected]";
$transport = (new SmtpTransport(\Config::get('mail.mailers.smtp.host'), \Config::get('mail.mailers.smtp.port'), null))->setUsername(\Config::get('mail.mailers.smtp.username'))->setPassword(\Config::get('mail.mailers.smtp.password'));
$mailer = new Swift_Mailer($transport);
Mail::setSwiftMailer($mailer);
Mail::send('emails.emailhtmlpage', [], function ($m) use ($from_email,$email_name,$to_email) {
$m->from($from_email, $email_name);
$m->to($to_email, 'TEST')->subject("Your Email Subject");
});
Upvotes: 1
Reputation: 10176
The settings applied using Config::set
do not persist across different requests: if you only run them when the user logins, the changes with not apply to the following requests.
You should modify your Authenticate
middleware adding:
public function handle($request, Closure $next, ...$guards)
{
$authReturned = parent::handle($request, $next, $guards);
$user = Auth::user();
\Config::set(['mail.mailers.smtp.host' => $user->smtp_mail_host]);
\Config::set(['mail.mailers.smtp.port' => $user->smtp_mail_port]);
\Config::set(['mail.mailers.smtp.username' => $user->smtp_mail_username]);
\Config::set(['mail.mailers.smtp.password' => $user->smtp_mail_password]);
\Config::set(['mail.from.address' => $user->mail_address]);
\Config::set(['mail.from.name' => $user->mail_from_name]);
return $authReturned;
}
Upvotes: 0