有人可以帮我处理这个邮件脚本吗?
我只是想发送一封 html 电子邮件,部分消息来自用户文本区域,其中输入\r\n。
我似乎无法使用 nl2br 或任何其他类似的功能。下面的代码不是我正在使用的,但仍然会产生错误。
代码:
$to = 'example@gmail.com';
$subject = 'Test Subject';
$message_var_1 = 'test1 \r\n test2 \r\n test3';
$message = nl2br("
<div>
<div>$message_var_1</div>
</div>
");
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'X-Mailer: PHP/'.phpversion() . "\r\n";
mail($to, $subject, $message, $headers);
最佳答案
$message_var_1 = 'test1 \r\n test2 \r\n test3';
PHP 仅在 "
内解析 \r
和 \n
,而不是在 '
内。因此 nl2br
在这里不适用。
$message_var_1 = "test1 \r\n test2 \r\n test3";
$message = '<div>
<div>'.nl2br($message_var_1).'</div>
</div>';
这应该有效。
关于PHP nl2br() 基本函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32634102/