gpt4 book ai didi

Laravel 5.5 邮件中包含用户名

转载 作者:行者123 更新时间:2023-12-02 21:17:52 25 4
gpt4 key购买 nike

我想我错过了一些非常简单的东西,但我有一个像这样的脚本:

\Mail::to( User::all() )
->send( new NotificationEmail($notification) );

class NotificationEmail extends Mailable {
use Queueable, SerializesModels;

/**
* Create a new message instance.
*
* @param Notification $notification
*
*/
public function __construct( Notification $notification ) {
$this->notification = $notification;
}

/**
* Build the message.
*
* @return $this
*/
public function build() {
$notification = $this->notification;

return $this
->from( [
'address' => \env( 'MAIL_DEFAULT_SENDER' ),
'name' => \env( 'APP_NAME' )
] )
->view( 'email.notification.ready' );
}
}

现在我希望电子邮件以类似的内容开头

Dear {firstname of the user} But I have no idea how to get the firstname of user who is going to get that email. Is there any way to figure that out?

最佳答案

这不是向所有用户发送电子邮件的推荐方式,因为接收电子邮件的人可以看到所有收件人,并且他们会收到相同的消息,您无法自定义该消息以添加姓名用户。

您需要为每个用户创建单独的Mailable,并对所有Mailable进行排队。分别向所有用户发送电子邮件是一项耗时的任务,需要工作人员在后台处理队列。

$users = User::all();
foreach ($users as $user) {
Mail::to($user)->queue(new NotificationEmail($notification, $user));
}

现在您可以传递 $user 实例,并且用户的名字在 View 上可用:

public function __construct( Notification $notification , User $user) {
$this->notification = $notification;
$this->user = $user;
}

public function build() {
$notification = $this->notification;

return $this
->from( [
'address' => \env( 'MAIL_DEFAULT_SENDER' ),
'name' => \env( 'APP_NAME' )
] )
->view( 'email.notification.ready' , [
'user' => $this->user
]);
}

关于Laravel 5.5 邮件中包含用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46925506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com