PHP邮件换行问题

时间:2009-09-28 08:35:18

标签: php formatting email format

我想格式化邮件内容以显示不同行的内容。 这是我的消息背景。 在这种情况下,\ n和\ r不起作用。它只显示一行中的所有内容。

$message = 'Thank you for using . We really appreciate your business.'."\r\n".'If you are making your payment by mail, please make the check out to "blah blah" and send it to:'."\n".'blah blah '."\n".'blah blah'."\n".'San Gabriel, CA 91776'."\n".'Please see the attached invoice in PDF format for easy saving & printing purposes.';

$attachment = chunk_split(base64_encode($pdfdoc));
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/pdf; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
mail($_POST['add6'],$subject, $message, $headers);

我该怎么做?

4 个答案:

答案 0 :(得分:13)

您告诉电子邮件客户端邮件是HTML,因此CR LF组合将被视为任何其他空格。

要解决此问题,请更改内容类型以显示您要发送纯文本电子邮件

$headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"".$eol;

或者,将您的邮件转换为HTML邮件 - 在您的情况下,一种简单的方法是通过nl2br运行它以将换行符转换为<br>标记

答案 1 :(得分:5)

是的,你有Content-Type: text/html,因此CR LF被视为空格。可以将其作为Content-Type: text/plain发送,也可以在内容上发送nl2br

答案 2 :(得分:2)

您的内容类型为HTML,因此您应该使用br或p标记而不是换行符

答案 3 :(得分:0)

要使\ n工作,它需要是双引号,而不是单引号。 “\ n”是正确的,“\ n”是错误的,不起作用。