Laravel 5.3 - 邮件通知中的行之间没有中断?
我发邮件了通过toMail
通道,这是从一个文本区域,在那里我打新行中输入两次救了,所以它看起来像这样输入:Laravel 5.3 - 邮件通知中的行之间没有中断?
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
我变更的相关章节来源:
@foreach ($introLines as $line)
{{ $line }}
@endforeach
要:
@foreach ($introLines as $line)
{!! nl2br(htmlspecialchars($line)) !!}
@endforeach
但是,这仍然没有奏效。在分贝notifications
表,它被保存为:
Line 1\n\nLine 2 with space above and below\n\nLine 3
任何想法如何让发送的电子邮件显示的间距所以它不只是一个大斑点?
UPDATE:
所以laravel
插入与在notifications
表\n\n
通知,但它实际上是保存在model
表是这样的:
而这一点正是通过到email
,但仍不确定如何获得email
中的空间。
请指定您的电子邮件类型为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("[email protected]", "From1 Name123")
->view('super_admin.company-mail')->with(['data' => $this->company]);
}
}
我创造了这个可邮寄和工作正常,数据显示在表格格式。我和它的laravel 5.3。
只需添加一些HTML代码,以一个换到新行
@foreach ($introLines as $line)
<p>{{ $line }}</p>
// or use <br />
@endforeach
但增加新的换行符在降价,你可以这样做:
1 -
要有没有段落的换行符,您将需要使用两个 尾随空格和一个
enter
。
2 -
使用普通<br />
你输入文本包含换行符其中Laravel将自动去掉(参见Illuminate\Notifications\Messages\SimpleMessage
类的formatLine()
方法,其中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分割你的文字。由于“线”方法接受字符串或数组,你可以分割线,并通过他们的在一次:
return (new MailMessage)->subject('test spacing')
->line(array_map('trim', preg_split('/\\r\\n|\\r|\\n/', $this->text)));
您可以创建扩展MAILMESSAGE
像
class MyClassMailMessage extends MailMessage {
public function splitInLines($steps){
$arrSteps = explode("\n", $steps);
if(!empty($arrSteps)){
foreach ($arrSteps as $line) {
$this->with($line);
}
}
return $this;
}
}
类然后在你的通知中这样做
return (new MyClassMailMessage)
->splitInLines($this->text);
也许邮件是以html的形式发送的?如果是这样,请尝试添加'
'你想要空间的地方? – ImAtWar
是它正在发送预期的html。这就是为什么我同时尝试'{! nl2br($ line))!!}'和'{!! nl2br(htmlspecialchars($ line))!!}'但是仍然没有空间... – Wonka
nl2br只插入换行符。 HTML不能解释换行符。我认为最好的方法是在前后有一个'
'标签。 – ImAtWar