Laravel Notification is not mailed

4.3k Views Asked by At

i am using the notifications for the first time and got stuck on a problem. Implemented a "Welcome" Notification via "mail" and "database". The "database" thing is ok and works well. The Problem is the "mail" part. When triggering the notification via "artisan tinker", all is well and the mail is send (configured with "log" to write it to "laravel.log"). When using the exact same line of code like in Laravel, the db row is written, but no mail is send.

One word to the tinker: The log entry is NOT written in the moment i post my code on the command line, it is written to log when i say "quit" in tinker.

Any thoughts what went wrong???

Here is my Notification (Welcome.php):

<?php

namespace App\Notifications;

use App\Model\Account;
use App\Model\ClientSettings;
use App\Model\Mailserver;
use App\Model\Mailtemplate;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class Welcome extends Notification implements ShouldQueue
{
    use Queueable;

    private $user;
    private $mailserver;
    private $mailmessage;
    private $template;

    /**
     * Create a new notification instance.
     *
     * @param \App\Model\Account    $user
     * @param \App\Model\Mailserver $mailserver
     */
    public function __construct(Account $user, Mailserver $mailserver, Mailtemplate $mailtemplate)
    {
        $this->user = $user;
        $this->mailserver = $mailserver;
        $this->mailmessage = null;
        $this->template = $mailtemplate;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail', 'database'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        $clientsettings = ClientSettings::first();
        $maillogo = '';

        if ($clientsettings !== null) {
            $maillogo = $clientsettings->getMaillogo();
        }
       return (new MailMessage)
           ->subject($this->template->subject)
           ->greeting('Very nice Greeting ;)')
           ->salutation($maillogo)
           ->line('Willkommen bei X!')
           ->action('Anmelden', url(config('app.url')))
           ->line('have fun!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            'id' => $notifiable->getAttributes()['ac_id'],
            'email' => $notifiable->ac_email,
            'user' => $this->user,
            'mailserver' => $this->mailserver,
            'mailmessage' => ($this->mailmessage !== null) ?: $this->toMail($notifiable),
        ];
    }
}

The tinker command is:

Notification::send(App\User::find(1), new App\Notifications\Welcome(App\Model\Account::first(), App\Model\Mailserver::first(), App\Model\Mailtemplate::first()));

This is the code triggering (very quick and dirty)

    public function sendWelcomeNotification(Request $request): JsonResponse
    {
        $this->validator = WelcomeStoreRequest::class;
        $inputData = $request->validate(app($this->validator)->rules());
        $user = Account::findOrFail($inputData['user_id']);
        $server = array_key_exists('server_id', $inputData) ? Mailserver::findOrFail($inputData['server_id']) : Mailserver::first();
        $template = Mailtemplate::where('type', '=', 'WELCOME')->first();
//        $user->notify(new Welcome($user, $server, $template));
        Notification::send($user, new Welcome($user, $server, $template));
        return new JsonResponse([
            'status' => 'ok'
        ]);
    }

None of the 2 ways to notify is working :(

1

There are 1 best solutions below

0
On BEST ANSWER

Ok, found it by myself, i needed some minutes to end my facepalm, sry.

Reason was in the database "design", for the account table had an entry with "ac_email" and laravel searched for "email". So, adding this to my model file

public function routeNotificationForMail($notification)
{
    return $this->ac_email;
}

Done the trick and emails are now send. Thanks everybody for helping me out ;)