我通过创建函数storeEmail
将发送给实体的每封电子邮件保存到数据库中,并将MailMessage
类插入到EmailMessage
模型中。一切工作正常,主要的[[goal是在收件人收到消息后完全显示该消息,并将我以User
格式发送的所有消息检索到页面上。为了更容易在foreach循环中检索每个特定Message的呈现,我认为最好从Model中获取它。
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
- 我在$ email-> mail = $ mail中保存(新的MailMessage);
我试图 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编辑
works
:$email = EmailMessage::first();
return (new \App\Notifications\SimpleEmail('my-link', $email->recipient->assignto))->toMail($email->recipient);
,因为每次我需要查找:但这并不是我想要的