让生活变得困难,我工作的客户正在使用一个非常大但旧的系统,该系统运行在 PHP4.0 上,他们不希望添加任何额外的库。
我正在尝试通过 PHP 发送一封包含附件和随附文本/html 内容的电子邮件,但我无法在一封电子邮件中同时发送这两种内容。
这会发送附件:
$headers = "Content-Type: text/csv;\r\n name=\"result.csv\"\r\n Content-Transfer-Encoding: base64\r\n Content-Disposition: attachment\r\n boundary=\"PHP-mixed-".$random_hash."\"";
$output = $attachment;
mail($emailTo, $emailSubject, $output, $headers);
这会发送文本/html:
$headers = "Content-Type: text/html; charset='iso-8859-1'\r\n";
$output = $emailBody; // $emailBody contains the HTML code.
这会发送一个包含 text/html 的附件以及附件内容:
$headers = "Content-Type: text/html; charset='iso-8859-1'\r\n".$emailBody."\r\n";
$headers = "Content-Type: text/csv;\r\n name=\"result.csv\"\r\n Content-Transfer-Encoding: base64\r\n Content-Disposition: attachment\r\n boundary=\"PHP-mixed-".$random_hash."\"";
$output = $attachment;
我需要知道的是如何在电子邮件正文中发送带有 text/html 的电子邮件并向其添加附件。我确定我在这里遗漏了一些非常简单的东西!
提前致谢。
最佳答案
好吧,我终于破解了!供引用:
$emailBody .= "<html><body>Blah</body></html>";
$emailSubject = "Subject";
$emailCSV = "\"Col1\", \"Col2\"\r\n"; // etc.
$attachment = $emailCSV;
$boundary = md5(time());
$header = "From: Name <address@address.com>\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed;boundary=\"" . $boundary . "\"\r\n";
$output = "--".$boundary."\r\n";
$output .= "Content-Type: text/csv; name=\"result.csv\";\r\n";
$output .= "Content-Disposition: attachment;\r\n\r\n";
$output .= $attachment."\r\n\r\n";
$output .= "--".$boundary."\r\n";
$output .= "Content-type: text/html; charset=\"utf-8\"\r\n";
$output .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$output .= $emailBody."\r\n\r\n";
$output .= "--".$boundary."--\r\n\r\n";
mail($emailTo, $emailSubject, $output, $header);
关于php - 使用 mail() 在 PHP4 的电子邮件中发送附件和 text/html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14476479/