我使用了两个教程来创建通知我新用户注册的代码。 mail-notifications 和 redirect-login-register-add-method/
现在可以了
我只是向我的 User
模型中定义的默认电子邮件发送通知:
这是我想要的目标
我想在电子邮件通知中发送新注册用户的电子邮件。另外,我希望自定义此特定 HelloUser
通知的收件人。
public function routeNotificationForMail()
{
return '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="741a151911341319151d185a171b19" rel="noreferrer noopener nofollow">[email protected]</a>';
}
通知是由我的RegisterController.php
中的代码触发的:
protected function redirectTo()
{
$user = Auth::user();
$user->notify(new \App\Notifications\HelloUser());
return '/'; // redirects to main page
}
上述解决方案有效,但经过多次尝试,我仍然无法获得这些扩展结果:
要做1
我会收到几条通知,我想将其邮寄到 2-3 封电子邮件,而不仅仅是一封。
我的尝试
在我的通知文件 App\Notifications\\HelloUser.php
中,我尝试添加
额外的线。 (请注意,该行现在已被注释)
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('The new user email is ');
// ->to('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90fef1fdf5d0f7fdf1f9fcbef3fffd" rel="noreferrer noopener nofollow">[email protected]</a>');
}
...但是我失败了。我可以按照 here in the documentation 的描述定义全局收件人
我的问题#1
如何为每个通知
定义单独的收件人?
待办事项 2
我还尝试将新注册用户的电子邮件添加到电子邮件通知中。
为此,我尝试从 Mailables
复制我的解决方案。因此,在 RegisterControler.php
中,我尝试在函数中传递 $user 变量:
protected function redirectTo()
{
$user = Auth::user();
$user->notify(new \App\Notifications\HelloUser($user));
return '/';
}
然后在我的Notification
文件App\Notifications\HelloUser
中我这样做了:
public $user;
public function __construct($user)
{
$email = $user->email;
}
希望这篇文章能够生成带有新用户电子邮件的通知:
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('The new user email is '.$user->email);
}
结果:我刚刚得到 undefined variable :用户
。
待办事项
如何让这段代码工作?
感谢您的宝贵时间。
============
编辑:
根据@Arun_jai 的请求,我将 dd($notabilly) 放在这里。看起来它已正确生成,并且该对象只是 User
模型的实例。相关部分:
#attributes: array:6 [▼
"name" => "Peter attempt 16"
"email" => "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="781619151d381f15191114561b1715" rel="noreferrer noopener nofollow">[email protected]</a>"
"password" => "$2t9WHLFY14XNf0$nj7TYDYAxiZ/kdfrUy$1vC2"
"updated_at" => "2018-01-18 07:08:12"
"created_at" => "2018-01-18 07:08:12"
"id" => 270
]
最佳答案
关于 undefined variable :用户
错误,请尝试在构造函数中实例化$user
变量,而不是电子邮件。您的构造函数应如下所示:
public function __construct($user)
{
$this->user = $user;
}
然后,您将能够通过在该类的任何位置使用 $this->user 调用它来获取用户的电子邮件,因此您的 MailMessage 创建将如下所示:
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('The new user email is ' . $this->user->email);
关于php - 关于新用户注册的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48315462/