PHP中邮件正文中的MIME信息

4
我一直在尝试使用PHP邮件函数发送带有HTML正文和附件的电子邮件。没有附件,我可以正常接收HTML邮件,但当我尝试附加文件时,所有MIME信息都包含在正文中,包括编码的附件。
以下是不带附件的电子邮件函数代码 - 可以正常工作:
        $this->to = $to;
        $this->subject = $subject;
        $this->message = $message;
        $this->headers = "From: " . Mailer::FROM_EMAIL . "\r\n";
        $this->headers .= "MIME-Version: 1.0\r\n";
        $this->headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

以下是带附件的电子邮件版本的代码:
        $this->to = $to;
        $this->subject = $subject;
        $this->attachment = 
                chunk_split(base64_encode(file_get_contents($attachment)));        

        //create a boundary string. It must be unique 
        //so we use the MD5 algorithm to generate a random hash        
        $boundary = md5(date('r', time()));                
        $this->headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com\r\n";        
        $this->headers .= "MIME-Version: 1.0\r\n ";
        $this->headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-$boundary\"\r\n"; 

        $this->message =    
"--PHP-mixed-$boundary
Content-Type: text/html; charset=\"ISO-8859-1\"

" . $message . "


--PHP-mixed-$boundary
Content-Type: application/pdf; name=\"test.pdf\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

".$this->attachment ."
--PHP-mixed-$boundary--";

我正在使用这个函数发送电子邮件:
public function send(){

    if (preg_match(Mailer::PATTERN, trim(strip_tags($this->to)))) { 
        $cleanedTo = trim(strip_tags($this->to)); 
    } else { 
        return FALSE; 
    }                 
    return mail ($cleanedTo, $this->subject, $this->message, $this->headers); 
}

我创建Mailer对象的方式如下:

$mailer = new Mailer("youremail@gmail.com", "test mail", "Some <b>old good</b> HTML email", 'pdf/test.pdf');
//$mailer = new Mailer("youremail@gmail.com", "test mail", "Some <b>old good</b> HTML email");

$mailer->send();

我收到的电子邮件内容是:

--PHP-mixed-e37929b72bbc6f8e3b37cf802619aac1
Content-Type: text/html; charset="ISO-8859-1"

Some <b>old good</b> HTML email


--PHP-mixed-e37929b72bbc6f8e3b37cf802619aac1
Content-Type: application/pdf; name="test.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

JVBERi0xLjMKMSAwIG9iago8 ... FT0YK

--PHP-mixed-e37929b72bbc6f8e3b37cf802619aac1--

我认为我离答案很接近了,但是...我需要你的帮助找到它。

2个回答

2
$this->headers .= "MIME-Version: 1.0\r\n ";
$this->headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-$boundary\"\r\n"; 

在MIME-Version行的换行符后删除空格。在Content-Type前面的尾随空格将使其成为前一行的延续。

顺便说一下:如果您的代码在Linux/Unix上运行,请仅在每行末使用“\n”。


谢谢,Hägar!问题是我已经稍微改变了这个函数,现在它只能与Gmail一起使用...这让我疯狂难受 :-( - MiGU

0
请使用该标头:

Content-Type:multipart/mixed;

我建议你也看一下这个class


谢谢你的回答!事实上,我正在使用这个头文件,就像你在这里看到的一样:"$this->headers .= "Content-Type: multipart/mixed; boundary="PHP-mixed-$boundary"\r\n";" - MiGU
1
我使用了这个,它完美地工作了。我认为还有另一个问题。你检查了class吗? - Maysam
谢谢,Maysam!那个类对我也起作用了。然而,我仍然想知道我的以前的代码有什么问题,因为我花了很多时间尝试让它工作,当我无法解决问题时,我很讨厌这种情况。 :-( - MiGU

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接