1

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 :(

Paladin
  • 1,637
  • 13
  • 28
  • Can you post your code please? – Jonathon Sep 20 '18 at 08:00
  • Sure, but i am not sure what code you need. The notification? The configuration? – Paladin Sep 20 '18 at 08:01
  • Ideally, your notification class, the code that's triggering it and what you did in tinker for it to work. The configuration could also be useful too. Basically, anything that might be relevant :) – Jonathon Sep 20 '18 at 08:03
  • Ok, added some code, is there anything pointing to something i did wrong? – Paladin Sep 20 '18 at 08:05
  • I don't see anything straight away. The only thing I noticed is that you're doing `$request->validate(...);` and you're expecting that to return an array. As far as I know, the `Request` object does not have a `validate` method and most `validate` methods in the framework tend not to return arrays. Is this something you've done yourself? The `FormRequest` class does have a validate method introduced by the `ValidatesWhenResolved` trait but that doesn't return anything. – Jonathon Sep 20 '18 at 08:11
  • @Jonathon No, this is a build-in laravel feature to manually validate with a Validator class. And it works well, in $inputData, i have only validated data without anything, that would not belong in there. – Paladin Sep 20 '18 at 08:22
  • Can you place `dd(get_class($request), $inputData);` *before* the line that begins with `$user = Account::findOrFail(...)` and edit your question to include its output, please? (Edit: Sorry, I messed that up, updated my comment) – Jonathon Sep 20 '18 at 08:26
  • @Jonathon here u are: https://picload.org/view/dlwawwca/image.png.html , made a ``dd($request, $inputData);`` – Paladin Sep 20 '18 at 08:29

1 Answers1

7

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 ;)

Paladin
  • 1,637
  • 13
  • 28