1

我第一次使用通知并遇到问题。通过“邮件”和“数据库”实现“欢迎”通知。“数据库”的东西没问题,运行良好。问题是“邮件”部分。当通过“artisan tinker”触发通知时,一切正常并发送邮件(配置“log”以将其写入“laravel.log”)。当使用与 Laravel 中完全相同的代码行时,会写入 db 行,但不会发送邮件。

给修补匠的一句话:日志条目不是在我在命令行上发布代码的那一刻写入的,它是在我在修补匠中说“退出”时写入日志的。

有什么想法出了什么问题???

这是我的通知(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),
        ];
    }
}

修补程序命令是:

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

这是代码触发(非常快速和肮脏)

    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'
        ]);
    }

两种通知方式都不起作用:(

4

1 回答 1

7

好的,我自己找到的,我需要几分钟才能结束我的手掌,sry。

原因在数据库“设计”中,因为帐户表有一个带有“ac_email”的条目,而 laravel 搜索了“电子邮件”。因此,将其添加到我的模型文件中

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

大功告成,现在发送电子邮件。感谢大家帮助我;)

于 2018-09-20T08:35:47.717 回答