php Laravel 5 将 HTML 添加到电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29891315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 adding HTML to email
提问by Devos50
I'm currently trying to create a HTML email in Laravel 5 and I have some text (which contains <br/>
elements) I want to insert in the email. I use the following piece of code to send the email:
我目前正在尝试在 Laravel 5 中创建 HTML 电子邮件,并且<br/>
我想在电子邮件中插入一些文本(其中包含元素)。我使用以下代码发送电子邮件:
Mail::send(array('html' => 'emails.newinvoice'), array('text' => $emailtext), function($message) use ($email, $subject, $contact_company)
{
$message->to($email, $contact_company)->subject($subject);
});
So the $emailtext
variable contains some text with HTML tags. In my emails.newinvoice
layout view, I have the following:
因此该$emailtext
变量包含一些带有 HTML 标签的文本。在我的emails.newinvoice
布局视图中,我有以下内容:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head></head>
<body>
<p>{{{ $text }}}</p>
</body>
</html>
When sending the email, the inserted text in my mail and the HTML elements in that text are displayed as normal characters which means that my email shows up like:
发送电子邮件时,我的邮件中插入的文本和该文本中的 HTML 元素显示为普通字符,这意味着我的电子邮件显示如下:
test<br/>test
Instead of
代替
test
test
How can I make sure that the HTML tags in the inserted text get rendered as HTML and not as plain text?
如何确保插入文本中的 HTML 标签呈现为 HTML 而不是纯文本?
回答by Limon Monte
You need to specify html
key in the first parameter:
您需要html
在第一个参数中指定键:
Mail::send( ['html' => 'emails.newinvoice'], ['text' => $emailtext],
// ^^^^
Also replace auto-escaped block {{ }}
with unescaped {!! !!}
in the template:
还要用模板中的{{ }}
unscaped替换 auto-escaped 块{!! !!}
:
<p> {!! $text !!} </p>
回答by Igor Pantovi?
You need to use:
您需要使用:
{!! $text !!}
instead of
代替
{{ $text }}
Blade automatically escapes any html when echoing unless you explicitly tell it not to.
Blade 在回显时会自动转义任何 html,除非您明确告诉它不要。
回答by Delino
Yeah the solution above work just fine..
是的,上面的解决方案工作得很好..
use {!! $contents !!}
用 {!! $contents !!}
instead of this
而不是这个
{{ $contents }}
This {!! $contents !!}
is for allowing html
While this {{ $contents }}
is just for plain text.
这{!! $contents !!}
是为了允许 html 而这{{ $contents }}
仅用于纯文本。
回答by Zeeweesoft
After checking various solutions, following codes worked for me -
检查各种解决方案后,以下代码对我有用-
try {
$template_data = ['otp' => $otp, 'name' => $name];
//send verification code
Mail::send(['html' => 'email.account_verification'], $template_data,
function ($message) use ($email) {
$message->to($email)
->from('[email protected]') //not sure why I have to add this
->subject('Account verification');
});
return Response::json(['code' => 200, 'msg' => 'Sent successfully']);
} catch (Exception $ex) {
return Response::json(['code' => 200, 'msg' => 'Something went wrong, please try later.']);
}