我通过创建一个函数storeEmail
并将MailMessage
类插入到EmailMessage
模型中,将发送给实体的每封电子邮件保存到数据库中。一切正常,主要目标是在收件人收到消息时完全按原样显示消息,并将我作为User
, 发送的所有消息检索到页面。为了更容易在 foreach 循环中检索每个特定消息的渲染,我认为最好从模型中获取它。
这是我的通知类:
class SimpleEmail extends Notification
{
use Queueable;
private $link;
private $user;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($link)
{
$this->link = $link;
$this->user = Auth::user();
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$mail = (new MailMessage)
->from($this->user->email, $this->user->name)
->subject('My Dummy Subject')
->greeting('To: '.$notifiable->email)
->action('Action Button', url($this->link))
->line('Thank you for reading my message')
->salutation('Friendly, '.$this->user->name);
$this->storeEmail($mail,$notifiable);
return $mail;
}
public function storeEmail($mail,$notifiable){
$email = new EmailMessage;
$email->sender_type = 'App\User';
$email->sender_id = $this->user->id;
$email->mail = $mail;
$email->save();
$notifiable->email_messages()->save($email);
}
}
Note:
- 我正在使用
Illuminate\Notifications\Messages\MailMessage
- 我的课延伸
Illuminate\Notifications\Notification
- 我将(新 MailMessage)保存在 $email->mail = $mail;
我试过了dd($email->mail);
,我得到了这个:
^ array:20 [▼
"view" => null
"viewData" => []
"markdown" => "notifications::email"
"theme" => null
"from" => array:2 [▶]
"replyTo" => []
"cc" => []
"bcc" => []
"attachments" => []
"rawAttachments" => []
"priority" => null
"callbacks" => []
"level" => "info"
"subject" => "My Dummy Subject"
"greeting" => "To: Dohn John"
"salutation" => "Friendly, Nikolas Diakosavvas"
"introLines" => array:2 [▶]
"outroLines" => array:1 [▶]
"actionText" => "Action Button"
"actionUrl" => "http://my-example-url.com ▶"
如何显示邮件通知,就像我发送它时一样?什么是最佳解决方案?提前致谢
已编辑
使用此代码管理渲染 MailMessage工作:
$email = EmailMessage::first();
return (new \App\Notifications\SimpleEmail('my-link', $email->recipient->assignto))->toMail($email->recipient);
但这并不是我想要的,因为每次我都需要找到:
- 每封电子邮件都使用哪个通知类,以便我可以呈现它。
- 每个通知类的变量。