简体   繁体   中英

Changing sender name and email laravel

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

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;
    }

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   = "test@test.com";
$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");
});

if you using Queue and Jobs make sure to

  • stop the php artisan queue:work first (its seems to have it own cache where it store the configs)
  • check background process to make sure 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 )
  • do the change you want in.env or configs
  • config:clear
  • redo php artisan queue:work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM