我的网站有一个奇怪的问题。我有几个正确发送的通知,例如电子邮件验证和密码重置。但是,我制作了自己的通知,将带有 UUID 的 url 发送给用户,不幸的是,它没有发送。
我尝试了各种方法:'Notification::route',直接通知用户,没有任何效果。实际上,直接通知用户是不好的,因为我需要将它发送到一个未附加到任何模型的电子邮件地址。
无论如何,这是通知的代码。请记住其他通知有效,所以我怀疑这是问题所在。
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class NewEmail extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($uuid)
{
$this->uuid = $uuid;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line(__('messages.newEmailAsked'))
->action(__('messages.newEmailConfirm'), config('app.frontend_url') . '/verify-new-email?code=' . $this->uuid)
->line(__('messages.newEmailIgnore'));
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
public function addNewEmail($email) {
if(User::where('email', $email)->count() === 0) {
$uuid = Str::uuid();
NewEmail::create(['email' => $email, 'unique_code' => $uuid, 'user_id' => $this->id]);
Notification::route('mail', $email)->notify(new \App\Notifications\NewEmail($uuid));
} else {
return 'Email already exists.';
}
}
我真的不明白为什么不发送此通知而其他通知...
最佳答案
奇怪的是,当我执行用户将采用的实际路径而不是使用 Tinker 时,问题自行解决了。
只需要 $user->addNewEmail($newEmailHere) 就可以正常工作...
关于php - 我的自定义邮件通知未发送,而其他所有通知均已发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63925289/