Laravel 5.4^ - How to customize notification email layout?

前端 未结 8 943
生来不讨喜
生来不讨喜 2020-12-01 08:07

I am trying to customize the HTML email layout that is used when sending notifications via email.

I have published both the mail and notification views.

8条回答
  •  日久生厌
    2020-12-01 08:42

    I ended up just using a custom view rather than trying to get the built in Laravel ones to work.

    I added the following use statement to my Notification class

    use Illuminate\Support\Facades\View;
    use Illuminate\Support\HtmlString;
    use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
    

    Then in the toMail method:

    public function toMail($notifiable)
        {
            $view_file = 'emails.teamInvitation';
            $view = View::make($view_file, ['sender' => $this->sender, 'invitationToken' => $this->invitationToken, 'team' => $this->team ]);
    
            $view =  new HtmlString(with(new CssToInlineStyles)->convert($view));
    
            return (new MailMessage)
                ->subject('PreSource Invitation From ' . $this->sender->name )
                ->view('emails.htmlBlank', ['bodyContent' => $view]);
        }
    

    emails.teamInvitation is my actual email template.

    I compile the view in to a string, and then convert the stylesheets to be inline.

    emails.htmlBlank is a view file but all it does is echo out bodyContent. This is necessary because the MailMessage->view method expects a view file, and not an HtmlString.

提交回复
热议问题