我在使用 php 发送邮件时遇到换行符问题。这里的主要问题是,换行符在带有附件(多部分/混合)的邮件中不起作用,但在纯文本中它们起作用。
<?php
function sendMail($email, $name, $pdfpath) {
// $file should include path and filename
$filename = basename($pdfpath);
$file_size = filesize($pdfpath);
$content = chunk_split(base64_encode(file_get_contents($pdfpath)));
$uid = md5(uniqid(time()));
$subject = "New attachment mail: ".$name;
$subject = "=?UTF-8?B?".base64_encode($subject)."?=";
$body = "Hello Dude!nn"
."Testingnn"
."Testingnnnn"
."Testingrn"
."Testingn";
//combine two headers (attachment mails needs to have specific headers)
$header .= $this->attachment_headers;
$header .= "Content-Type: multipart/mixed; boundary="".$uid.""rnrn";
$header .= "This is a multi-part message in MIME format.rn";
$header .= "--".$uid."rn";
$header .= "Content-type:text/plain; charset=UTF-8rn";
$header .= "Content-Transfer-Encoding: 8bitrnrn";
$header .= $body."rnrn";
$header .= "--".$uid."rn";
$header .= "Content-Type: application/octet-stream; name="".$filename.""rn"; // use different content types here
$header .= "Content-Transfer-Encoding: base64rn";
$header .= "Content-Disposition: attachment; filename="".$filename.""rnrn";
$header .= $content."rnrn";
$header .= "--".$uid."--";
//( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
return mail($email, $subject, "", $header);
}
?>
因此,我的电子邮件中的输出如下:
Hello Dude!
Testing
Testing
Testing
Testing
所以,它确实添加了一行新行,但只有一行,我需要更多。我该怎么做?
我认为你应该使用rnrn
而不是nn
。
我知道
art2 答案被标记为正确,但我会在这一行中使用 nl2br:
$header .= nl2br($body)."\r\r";
只是想帮忙...
好的,我无法从网络上找到任何帮助,所以我想出了一个泡泡糖解决方案。似乎由于某种原因,多部分/混合消息条/删除了所有其他行。
$body = "Hello Dude!n n"
."Testingn n"
."Testingn n n n"
."Testingr n"
."Testingn";
查看换行符之间的空格,结果是预期的。