在浏览器中预览邮件通知

Preview Mail Notification in browser

使用 Laravel,根据 to the documentation,我可以通过控制器 return 和 Mailable 在浏览器中显示它。它有助于预览邮件。

有没有办法在浏览器中预览邮件通知?

我试过了:

return (new MyNotification())->toMail($some_user);

但是不行:

The Response content must be a string or object implementing __toString(), "object" given.

您无法渲染在 toMail() 中使用的 Notification. You can render Mailable。例如,如果该 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('example@gmail.com');    
 $markdown = new \Illuminate\Mail\Markdown(view(), config('mail.markdown'));

return $markdown->render('vendor.notifications.email', $message->data());

只需更改通知的名称 class(并在必要时传递参数)并在浏览器中点击 url 以查看预览。

对我来说,我想要预览 toMail() 方法的特定通知需要一个 Notifiable 实例,而不仅仅是一个电子邮件地址,所以下面的代码对我有用:

    $notification = new \Illuminate\Auth\Notifications\VerifyEmail();

    $user = \App\User::where('email', 'example@gmail.com')->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());