我一直在尝试使用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--
我认为我离答案很接近了,但是...我需要你的帮助找到它。