Saturday, 19 September 2020

My custom Mail Notification isn't sending while all the other are

I have a weird issue in my website. I have several Notifications such as Email Verification and Password Reset that are sending properly. However, I made my own notification that sends an url with a UUID to the user and unfortunately, it doesn't send.

I tried every way: 'Notification::route', notify the user directly, nothing works. Actually, notifying the user directly would be bad since I need to send it to an email address not attached to any model.

Anyway, here's the code for the notification. Keep in mind the other notifications work, so I doubt it is the issue.

<?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.';
        }
    }

I really don't get why this notification isn't sent while the other are...



from My custom Mail Notification isn't sending while all the other are

No comments:

Post a Comment