为什么双线换行\ n \ n在附件邮件中不起作用?

时间:2013-01-30 20:48:00

标签: php email utf-8 newline attachment

我在使用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!\n\n"
             ."Testing\n\n"
             ."Testing\n\n\n\n"
             ."Testing\r\n"
             ."Testing\n";
        //combine two headers (attachment mails needs to have specific headers)
        $header .= $this->attachment_headers;
        $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
        $header .= "This is a multi-part message in MIME format.\r\n";
        $header .= "--".$uid."\r\n";
        $header .= "Content-type:text/plain; charset=UTF-8\r\n";
        $header .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
        $header .= $body."\r\n\r\n";
        $header .= "--".$uid."\r\n";
        $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
        $header .= "Content-Transfer-Encoding: base64\r\n";
        $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
        $header .= $content."\r\n\r\n";
        $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

所以,它确实添加了一个新行,但只有一行,我需要更多。我怎么能这样做?

3 个答案:

答案 0 :(得分:0)

我认为您应该使用\r\n\r\n代替\n\n

答案 1 :(得分:0)

好的,我无法从网上找到任何帮助,所以我想出了一个bubblegum解决方案。由于某种原因,多部分/混合消息条/删除所有其他行似乎是这样。

 $body = "Hello Dude!\n \n"
             ."Testing\n \n"
             ."Testing\n\ n\ \n \n"
             ."Testing\r \n"
             ."Testing\n";

查看换行符之间的空格,结果是应该的。

答案 2 :(得分:0)

我知道art2的答案被标记为正确,但我会在这一行中使用nl2br:

$ header。= nl2br($ body)。“\ r \ n \ r \ n”;

试图帮助...