Laravel 通知未邮寄
Laravel Notification is not mailed
我是第一次使用通知,遇到了一个问题。
通过 "mail" 和 "database" 实现了 "Welcome" 通知。
"database" 没问题,效果很好。
问题是 "mail" 部分。通过 "artisan tinker" 触发通知时,一切正常,邮件已发送(配置 "log" 将其写入 "laravel.log")。当使用与 Laravel 中完全相同的代码行时,会写入数据库行,但不会发送任何邮件。
对修补匠说一句话:日志条目不是在我post我在命令行上的代码时写入的,当我在修补匠中说"quit"时它被写入日志。
想知道哪里出了问题吗???
这是我的通知 (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'
]);
}
2 种通知方式中的 None 有效:(
好的,我自己找到的,我需要几分钟来结束我的脸,sry。
原因在数据库 "design" 中,因为帐户 table 有一个包含 "ac_email" 的条目并且 laravel 搜索了 "email"。
所以,将其添加到我的模型文件
public function routeNotificationForMail($notification)
{
return $this->ac_email;
}
完成技巧,现在可以发送电子邮件了。
感谢大家帮助我 ;)
我是第一次使用通知,遇到了一个问题。 通过 "mail" 和 "database" 实现了 "Welcome" 通知。 "database" 没问题,效果很好。 问题是 "mail" 部分。通过 "artisan tinker" 触发通知时,一切正常,邮件已发送(配置 "log" 将其写入 "laravel.log")。当使用与 Laravel 中完全相同的代码行时,会写入数据库行,但不会发送任何邮件。
对修补匠说一句话:日志条目不是在我post我在命令行上的代码时写入的,当我在修补匠中说"quit"时它被写入日志。
想知道哪里出了问题吗???
这是我的通知 (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'
]);
}
2 种通知方式中的 None 有效:(
好的,我自己找到的,我需要几分钟来结束我的脸,sry。
原因在数据库 "design" 中,因为帐户 table 有一个包含 "ac_email" 的条目并且 laravel 搜索了 "email"。 所以,将其添加到我的模型文件
public function routeNotificationForMail($notification)
{
return $this->ac_email;
}
完成技巧,现在可以发送电子邮件了。 感谢大家帮助我 ;)