0

I have notifications that implement ShouldBroadcast and ShouldQueue (I'm using Redis and laravel-echo-server to broadcast notifications). I have to send mail too. So I have toMail method to send the user an email. But the problem is that the email is not sending!

I double-checked everything! My mail server configuration is correct (I tested it with another way of sending email), my notification is working well and as I know, everything is OK! but the email is not sending!

For a better explanation, I'll send the code here! In my TicketController, I have an instance of Ticket model (that uses Notifiable), so I'm using this line of code to send the notification:

$ticket->notify(new AdminTicketCreated());

and in my notification class:

class AdminTicketCreated extends Notification implements ShouldBroadcast, ShouldQueue
{
    use Queueable, SerializesModels;

    public function via($notifiable): array
    {
        return [SmsChannel::class, 'mail', 'broadcast', UserDatabaseChannel::class];
    }

    public function toMail($notifiable): MailMessage
    {
        $user = $notifiable->user;

        return (new MailMessage)
            ->subject('ارسال تیکت توسط ادمین')
            ->replyTo($user->email, $user->name)
            ->view('emails.ticket_send_admin', ['user' => $user, 'ticket' => $notifiable]);
    }
.
.
.
.
.
}

One of the problems I have is that I don't know how to debug it! Just before return (new MailMessage)... I Logged $notifiable and $user and their values are correct. But in the view, I'm using Log to check if it's rendering or not, and unfortunately, it's not working!

I'll be appreciated any response. Thank you :)

Update: My job is running with php artisan queue:listen. So, when I change my code I don't need to rerun it. Also, my laravel-echo-server is running too.

  • I created a Mailable class instead of MailMessage and now it's working and sending the emails! But I don't know why! The MailMessage way was working till two days ago and now it's not working anymore? :|
Ashkan
  • 47
  • 1
  • 8

1 Answers1

0

It could be that your jobs are not being run. Run artisan queue:work and see if they are being committed.

whizboy
  • 49
  • 7
  • My bad! I forgot to say that, my job is working. Also, my `laravel-echo-server` is running too. I created a Mailable class right now, and, it is working and sends emails!! But why? how? I'm getting crazy. :\ – Ashkan Oct 16 '22 at 08:00
  • Could you send a more detailed log of what's happening. Add debug statements using the facade `Log::debug("your string");` then send the detailed log over here. Then try to run `artisan cache:clear` and `composer dump-autoload` to see if the MailMessage class will work. At times it may need that you just rewrite the thing. There was one time my work was failing just because I failed to save file with .php extension, yet vs was highlighting it as php so i had assumed everything was ok. – whizboy Oct 18 '22 at 05:04
  • Thanks for your feedback. Unfortunately, it's not working and not logging anything! I used Log facade to log and debug it but everything is working until a line before the `return` statement and the return itself is not working. I added Log to view and checked if it logs anything or not! in the `MailMessage` version, it didn't log anything but when I changed it to a `Mailable` class, it started working and logged what was expected. – Ashkan Oct 18 '22 at 06:41
  • yeap that line before 'return' statement is what you need to send us. that's where it breaks. Log your fundaments and send the log around that area. your log file is located under `/storage/logs` Also just try to send mail without queueing first see if it works then now hook it up with the rest. You can adjust your queue to sync first, you can edit this on your .env or the database config file under app/config folder – whizboy Oct 18 '22 at 08:51