根据to the documentation的Laravel,我可以通过控制器返回Mailable
以在浏览器中显示它。它有助于预览邮件。
有没有办法在浏览器中预览邮件通知?
我试过了:
return (new MyNotification())->toMail($some_user);
但它不起作用:
Response内容必须是实现__toString()的字符串或对象,给出“object”。
你无法渲染Notification。你可以渲染你在Mailable中使用的toMail()
。例如,如果那个Mailable被称为SomeMailable
:
public function toMail($user)
{
return (new SomeMailable($user))->to($user->email);
}
然后你可以渲染Mailable:
return new SomeMailable($some_user);
在你的控制器的功能:
$message = (new \App\Notifications\MyNotification())->toMail('[email protected]');
$markdown = new \Illuminate\Mail\Markdown(view(), config('mail.markdown'));
return $markdown->render('vendor.notifications.email', $message->data());
只需更改通知类的名称(并在必要时也传递参数)并点击浏览器中的URL以查看预览。
对我来说,我想要预览toMail()
方法的特定通知需要一个Notifiable
实例而不仅仅是一个电子邮件地址,所以以下代码对我有用:
$notification = new \Illuminate\Auth\Notifications\VerifyEmail();
$user = \App\User::where('email', '[email protected]')->first(); // Model with Notifiable trait
$message = $notification->toMail($user);
$markdown = new \Illuminate\Mail\Markdown(view(), config('mail.markdown'));
return $markdown->render('vendor.notifications.email', $message->toArray());
在Laravel 5.8中,您现在可以像使用Mailable一样预览它。
Route::get('mail-preview', function () {
return (new MyNotification())->toMail($some_user);
});
更多细节在这里:https://sampo.co.uk/blog/previewing-mail-notifications-in-laravel-just-got-easier
试试这个(Laravel 5.6之后的测试通过)
$message = (new \App\Notifications\YourNotification()->toMail($notifiable);
return app()->make(\Illuminate\Mail\Markdown::class)->render($message->markdown, $message->data());