Php 具有发送电子邮件功能的Laravel nl2br()

Php 具有发送电子邮件功能的Laravel nl2br(),php,email,blade,laravel-5.3,nl2br,Php,Email,Blade,Laravel 5.3,Nl2br,我使用的是Laravel 5.3,我正试图用nl2br()发送一封电子邮件。因此,我使用的是默认的电子邮件刀片文件,这是Laravel 5.3中新增的文件(vendor/notifications/email.blade.php) 但它不显示换行符。只有这一点: sttesttesttesttesttesttesttesttesttesttesttesttest<br /> <br /> <br /> <br /> testtesttesttest

我使用的是
Laravel 5.3
,我正试图用
nl2br()
发送一封电子邮件。因此,我使用的是默认的
电子邮件刀片文件
,这是
Laravel 5.3
中新增的文件(
vendor/notifications/email.blade.php

但它不显示换行符。只有这一点:

sttesttesttesttesttesttesttesttesttesttesttesttest<br /> <br /> <br /> <br /> testtesttesttesttesttesttesttesttesttesttesttesttesttesttest

不工作。

Laravel使用{{}自动转义字符串

对于laravel 4+使用
{{{{nl2br($line)}}}

对于laravel 5+使用
{!!nl2br($line)!!}


如果版本控制错误,请纠正我。

对于Laravel 4用户:

{{ nl2br(e($message)) }}
{!! nl2br(e($message)) !!}
e($x)
相当于
{{{{$x}}}

Laravel 5用户:

{{ nl2br(e($message)) }}
{!! nl2br(e($message)) !!}

e($x)
相当于
{{$x}

我知道这有点晚了,但对每个有同样问题的人来说。 如果您使用的是通知邮件,新行将不起作用(“\n”、“\r”、“\r\n”)

这是因为Laravel(对于
5.3
5.4
我可以确认)将这些从行中剥离

供应商\laravel\framework\src\illumb\Notifications\Messages\SimpleMessage.php

我解决这个问题的方法是用实际的html新行替换新行

我相信有更好的方法可以做到这一点,但重要的是不要忽略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))

}

toMail的公共功能($notifiable){
//获取您的邮件数据
$mail=新电子邮件(2);
$emailLine=$mail->getArray();
返回(新邮件)
->行(str_替换(数组(“\\r\\n”、“\\n”、“\\r”、“\r\n”、“\n”、“\r”)、“
”、$emailLine)) }

由于@Jamie正在使用通知邮件频道,因此可能的重复项将不起作用。Laravel实际上会在字符串进入
nl2br()之前删除所有新行符号。
。这是因为他正在使用通知邮件频道。这应该是最重要的答案,对我来说非常有效!
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))

}