php - 我的自定义邮件通知未发送,而其他所有通知均已发送

标签 php laravel

我的网站有一个奇怪的问题。我有几个正确发送的通知,例如电子邮件验证和密码重置。但是,我制作了自己的通知,将带有 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/

相关文章:

php - Laravel5 - 如何正确编写搜索查询字符串的路由?

javascript - 将包含重音符号的数组从 Laravel 传递到 vue.js

php - 为什么我的 MySQL INSERT 查询插入 3 个相同的行?

php - 尝试在 laravel 上运行 gulp 时出现错误

php preg_match_all 返回数组数组

php Memcache 类找不到

php - Laravel 5.0 中通过关系访问属性

javascript - 使用 JavaScript 将 Laravel Blade 模板附加/添加到当前 View

php - 用 DOMDocument 替换 HTML 中的标签

php - 如何制作 jquery css php 进度条(倒计时)?