我正在使用Laravel 5.3,并且正在尝试使用nl2br()发送电子邮件。所以我使用的是Laravel 5.3中新增的默认email blade file (vendor/notifications/email.blade.php)
但它不显示换行符。只有这一点:
sttesttesttesttesttesttesttesttesttesttesttesttest<br /> <br /> <br /> <br /> testtesttesttesttesttesttesttesttesttesttesttesttesttesttest我是这样做的:
<!-- Outro -->
@foreach ($outroLines as $line)
<p style="{{ $style['``'] }}">
{{ nl2br($line) }}
</p>
@endforeach我做错了什么?
这一点:
{!! nl2br(htmlspecialchars($line)) !!}不起作用。
发布于 2016-11-03 20:32:41
对于Laravel 4用户:
{{ nl2br(e($message)) }}e($x)等同于{{{ $x }}}。
Laravel 5用户:
{!! nl2br(e($message)) !!}e($x)等同于{{ $x }}。
发布于 2016-11-03 20:28:05
Laravel通过使用{{ }}自动转义字符串
对于laravel 4+,请使用{{{ nl2br($line) }}}
对于laravel 5+,请使用{!! nl2br($line) !!}
如果我在版本控制上错了,请纠正我。
发布于 2017-03-14 16:50:02
我知道这有点晚了,但对于每个有同样问题的人来说。如果您正在使用通知邮件,换行符将不起作用("\n“、"\r”、"\r\n")。
这是因为Laravel (对于5.3和5.4,我可以确认)从行中去掉了这些
vendor\laravel\framework\src\Illuminate\Notifications\Messages\SimpleMessage.php
protected function formatLine($line)
{
if (is_array($line)) {
return implode(' ', array_map('trim', $line));
}
return trim(implode(' ', array_map('trim', preg_split('/\\r\\n|\\r|\\n/', $line))));
}我解决这个问题的方法是用实际的html新行<br>替换新行。
我相信有更好的方法可以做到这一点,但重要的是不要忽略formatLine函数。
public function toMail($notifiable) {
//Get your mail data
$mail = new Email(2);
$emailLine = $mail->getArray();
return (new MailMessage)
->line(str_replace(array("\\r\\n", "\\n", "\\r","\r\n", "\n", "\r"), "<br>", $emailLine))
}
https://stackoverflow.com/questions/40401402
复制相似问题