我可以只发送文本,只发送html,但是当我像下面这样将两者组合在一起时,电子邮件发送,但正文是空的。
$to = "some@email.com";
$subject = "Welcome";
$boundary = md5(date('U'));
$headers = "From: Company <noreply@email.com>" . "'n".
"X-Mailer: PHP/".phpversion() ."'n".
"MIME-Version: 1.0" . "'n".
"Content-Type: multipart/alternative; boundary=".$boundary. "'n";
"Content-Transfer-Encoding: 7bit". "'n";
// TEXT EMAIL PART
$text = "Congratulations";
// HTML EMAIL PART
$html = "<html><body>'n";
$html .= "<div>Congratulations</div>";
$html .= "</body></html>'n";
$message = "Multipart Message coming up" . "'n'n".
"--".$boundary.
"Content-Type: text/plain; charset='"iso-8859-1'"" .
"Content-Transfer-Encoding: 7bit".
$text.
"--".$boundary.
"Content-Type: text/html; charset='"iso-8859-1'"".
"Content-Transfer-Encoding: 7bit".
$html.
"--".$boundary."--";
mail($to, $subject, $message, $headers);
您忘记了在content - type和content - transfer - encoding行之后使用换行符,因此正文内容的开头与标题混合在一起:
"--".$boundary.
^^^--- missing 'n
"Content-Transfer-Encoding: 7bit".
^^^--- missing 'n'n
$text.
如上所述,使用Swiftmailer或PHPMailer来完成此操作。它们会处理所有琐碎的小细节,让你用更少的代码行发送整封邮件。
$boundary
不要处理MIME。