使用自定义HTML在Laravel中发送电子邮件

Car*_*oce 5 php email laravel

我需要发送电子邮件,但我已经生成了HTML,我不想使用laravel blade,因为我需要将CSS内联应用于HTML,所以这就是我生成html的方式:

getRenderedView($viewData) {
    //code
    $html =  View::make('email.notification', $viewData)->render();
    $this->cssInliner->setCSS($this->getCssForNotificationEmail());
    $this->cssInliner->setHTML($html);
    return $this->cssInliner->convert();
}
Run Code Online (Sandbox Code Playgroud)

因此,要使用Laravel发送邮件,您通常会执行以下操作:

Mail::send('emails.welcome', $data, function($message)
{
    $message->to('foo@example.com', 'John Smith')->subject('Welcome!');
});
Run Code Online (Sandbox Code Playgroud)

但是我不想传递视图,我已经有了html,我该怎么做?

小智 13

如果我对你想要实现的目标是正确的,为了解决这个问题,我创建了一个名为echo.php的视图,其中只有echo $ html.

将你的html分配给$ data ['html'].

然后将$ data ['html']传递给echo视图.

Mail::send('emails.echo', $data, function($message) { $message->to('foo@example.com', 'John Smith')->subject('Welcome!'); });

让我知道你是怎么办的.


Ken*_*mer 5

我想分享一个技巧,它可能有助于在没有“刀片”的情况下发送电子邮件。

Laravel Mail 函数实际上是 Swift 的包装器。只需将空数组分配给 $template 和 $data,我的意思是函数的前 2 个参数,然后在回调中执行其余操作。

Mail::send([], [], function($message) use($to, $title, $email_body)
{
    $message->setBody($email_body)->to($to)->subject($title);
});
Run Code Online (Sandbox Code Playgroud)