Laravel 5.3 - 邮件通知中的行之间没有中断?

时间:2017-03-31 19:14:57

标签: php laravel laravel-5 laravel-5.3

我通过toMail频道发送了一封电子邮件,该频道是从我为新线路输入两次的文字区域保存的,所以在输入中看起来像这样:

Line 1

Line 2 with space above and below

Line 3

当我在邮件中使用这样的文字时:

return (new MailMessage)->subject('test spacing')
                        ->line($this->text);

在电子邮件中看起来像这样:

Line 1 Line 2 with space above and below Line 3

我更改了email.blade.php中的相关部分:

@foreach ($introLines as $line)
  {{ $line }}
@endforeach

要:

@foreach ($introLines as $line)
  {!! nl2br(htmlspecialchars($line)) !!}
@endforeach

但那仍然没有奏效。在db中的notifications表中,它保存为:

Line 1\n\nLine 2 with space above and below\n\nLine 3

任何想法如何让发送的电子邮件显示间距,以便它不仅仅是一个大blob?

更新

因此,laravel会在\n\n表格中插入notifications的通知,但它实际上会保存在model表中,如下所示:

enter image description here

传递给email的是什么,但仍不确定如何在email中获取这些空格。

4 个答案:

答案 0 :(得分:1)

您可以创建一个扩展MailMessage

的类

喜欢

const element = ['abc','def','ghi'];
const value = 2;

return (    
<div onClick={ () => props.onClicked(value) } key={value}> 
 <p> {element[0]}{element[1]}{element[2]} </p>
</div>
);

然后在您的通知中执行此操作

class MyClassMailMessage extends MailMessage {

    public function splitInLines($steps){
        $arrSteps = explode("\n", $steps);
        if(!empty($arrSteps)){
            foreach ($arrSteps as $line) {
               $this->with($line);
            }
          }
         return $this; 
    }
}

答案 1 :(得分:0)

将您的电子邮件类型指定为text/html。我认为laravel自动但更好地使用我认为的观点。以下是为我工作。 另一种简单的方法是为您的邮件文本创建视图并使用它 使用视图按如下方式创建Mailable。

class CompanyCreated extends Mailable
{
    use Queueable, SerializesModels;

    public $company;

    public function __construct(Company $company)
    {
        $this->company = $company;
    }

    public function build()
    {
        return $this->subject("Alert: New Company added")
                ->from("fax@netinkvoice.com", "From1 Name123")
                ->view('super_admin.company-mail')->with(['data' => $this->company]);
    }
}

我创建了这个可邮寄的工作正常,数据以表格格式显示。我和它的laravel 5.3。

答案 2 :(得分:0)

您的输入文本包含Laravel将自动删除的换行符(请参阅formatLine()类的Illuminate\Notifications\Messages\SimpleMessage方法,MailMessage所依据的方法。您可以子类化MailMessage并覆盖格式:

class CustomMailMessage extends MailMessage {

    protected function formatLine($line) {
        if (is_array($line)) {
            return implode(' ', array_map('trim', $line));
        }
        // Just return without removing new lines.
        return trim($line);
    }

}

或者您可以在将文本发送到MailMessage之前拆分文本。由于“line”方法接受字符串或数组,因此您可以拆分行并将它们全部传入:

return (new MailMessage)->subject('test spacing')
    ->line(array_map('trim', preg_split('/\\r\\n|\\r|\\n/', $this->text)));

答案 3 :(得分:-1)

只需添加一些html标签即可换行新行

@foreach ($introLines as $line)
  <p>{{ $line }}</p>
  // or use <br />
@endforeach

但是要在降价中添加新的换行符,您可以这样做:
1 -

  

要有没有段落的换行符,您需要使用两个   尾随一个enter的空格。

source

2 -
使用普通<br />

来源:laravel docs