[英]Changing sender name and email laravel
For each user, I change the email configuration in laravel project, when user login i use this:对于每个用户,我在 laravel 项目中更改 email 配置,当用户登录时我使用这个:
\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.当我执行
config()
时,它显示了我的更改,但是当我发送时,邮件是从 .env 配置邮件地址发送的,缺少部分我找不到它。 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.使用
Config::set
应用的设置不会在不同的请求中持续存在:如果仅在用户登录时运行它们,则更改不适用于以下请求。
You should modify your Authenticate
middleware adding:您应该修改
Authenticate
中间件添加:
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:这不是最佳解决方案,但可以提供帮助:在调用发送方法之前将配置传递给您的 Mail::send,如下所示:
First import these classes in your Controller首先在您的 Controller 中导入这些类
use Mail;
use \Swift_Mailer;
use \Swift_SmtpTransport as SmtpTransport;
Then in your send function:然后在您发送 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如果您使用队列和作业,请确保
php artisan queue:work
first (its seems to have it own cache where it store the configs)php artisan queue:work
(它似乎有自己的缓存来存储配置)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 )queue:work
已经停止(对于 linux,您可以使用top -c -p $(pgrep -d',' -f php)
并搜索php artisan queue:work
in COMMAND 列)config:clear
php artisan queue:work
php artisan queue:work
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.