在浏览器中预览邮件通知

问题描述 投票:3回答:4

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

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

我试过了:

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

但它不起作用:

Response内容必须是实现__toString()的字符串或对象,给出“object”。

php laravel laravel-5 laravel-5.5
4个回答
2
投票

你无法渲染Notification。你可以渲染你在Mailable中使用的toMail()。例如,如果那个Mailable被称为SomeMailable

public function toMail($user)
{
    return (new SomeMailable($user))->to($user->email);
}

然后你可以渲染Mailable:

return new SomeMailable($some_user);

6
投票

在你的控制器的功能:

 $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以查看预览。


0
投票

对我来说,我想要预览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());

0
投票

在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


0
投票

试试这个(Laravel 5.6之后的测试通过)

$message = (new \App\Notifications\YourNotification()->toMail($notifiable);

return app()->make(\Illuminate\Mail\Markdown::class)->render($message->markdown, $message->data());
© www.soinside.com 2019 - 2024. All rights reserved.