在文本/普通电子邮件中添加换行符



我使用WordPresswp_mail()的功能来发送关于在我的网站上提交的表单数据的电子邮件。

$data中,我尝试使用n进行换行,但它在电子邮件上打印了n,如下所示:

First value n Second value n Third Value

我的代码是:

$data = "First: $first_value" . 'n' . "Second: $second_value" . 'n' . "Third: $third_value"

收到的电子邮件的期望结果是:

第一:价值

第二:价值

第三:价值

为了确保换行符("n")得到适当的实现,正如m.e eriksson建议的那样,在定义它们时必须避免使用单引号。要更清楚,请参见下面代码的输出:

$first_value = "one";
$second_value = "second";
$third_value = "third";
$data = "First: $first_value" . 'n' . "Second: $second_value" . 'n' . "Third: $third_value";
echo($data);
First: onenSecond: secondnThird: third

但是当你使用双引号(")而不是单引号(')时,结果将是:

<?php
$first_value = "one";
$second_value = "second";
$third_value = "third";
$data = "First: $first_value" . "n" . "Second: $second_value" . "n" . "Third: $third_value";
echo($data);
?>
First: one
Second: second
Third: third

更通用的方法是使用PHP_EOL:

<?php
$first_value = "one";
$second_value = "second";
$third_value = "third";
$data = "First: $first_value" . PHP_EOL . "Second: $second_value" . PHP_EOL . "Third: $third_value";
echo($data);
?>
First: one
Second: second
Third: third

最新更新