Laravel通知未邮寄

时间:2018-09-20 07:59:12

标签: laravel laravel-5 notifications laravel-5.6 laravel-notification

我是第一次使用通知,并陷入了问题。 通过“邮件”和“数据库”实现了“欢迎”通知。 “数据库”是可以的,并且运行良好。 问题是“邮件”部分。当通过“工匠修补匠”触发通知时,一切都很好,并且邮件已发送(配置为“ 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'
        ]);
    }

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

1 个答案:

答案 0 :(得分:3)

好吧,我一个人发现了,我需要几分钟才能结束我的脸部护理,对不起。

原因位于数据库“ design”中,因为帐户表中的条目带有“ ac_email”,而laravel搜索“ email”。 因此,将其添加到我的模型文件中

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

完成技巧,现在就可以发送电子邮件。 谢谢大家的帮助;)