【发布时间】:2011-12-20 13:53:08
【问题描述】:
我正在尝试创建一个 PHP 函数来使用带有附件的 SMTP 发送电子邮件。我很难创建 MIME 标头,因为我希望正文包含 HTM 格式的文本和要附加的文件。我使用的是非常旧版本的 PHP (4.3.8),这是唯一的方法那是有效的。尝试过 PEAR,但无法正确验证 SMTP。
这是我目前所拥有的: 我已经编辑了这段代码,因为现在我正确收到了消息,但文件已损坏。我把文件改成文本文件,一开始还不错,但后来出现了很多垃圾文本。
$newLine = "\r\n";
$attachment="myFile.zip";
$message = "<br><h1><center>TEST MESSAGE</center></h1>" ;
//Body
$headers .= "Content-Type: multipart/mixed;" . $newLine;
$headers .= " boundary=\"_e9e06aa5-1550-464d-ace4-e85b575d1899_\"" . $newLine . $newLine;
$newLine . $newLine;
$headers .= "--_e9e06aa5-1550-464d-ace4-e85b575d1899_" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "Content-Transfer-Encoding: quoted-printable" . $newLine;
$headers .= " boundary=\"_7fdd1316-6f68-41bb-93f7-134933fc9aad_\"" . $newLine . $newLine;
$headers .= $message . $newLine;
//$headers .= "--_7fdd1316-6f68-41bb-93f7-134933fc9aad_--" . $newLine; // If I leave this line it appears in the end of the message area.
//Attachment
$headers .= "--_e9e06aa5-1550-464d-ace4-e85b575d1899_" . $newLine;
$headers .= "Content-Transfer-Encoding: base64" . $newLine;
$headers .= "Content-Type: text/plain;" . $newLine;
$headers .= "Content-Disposition: attachment;" . $newLine;
$headers .= " filename=" . $attachment . $newLine . $newLine;
$handle = fopen($attachment, "rb");
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, filesize($attachment));
}
fclose($handle);
$contents = base64_encode($contents);
$headers .= $contents . $newLine;
$headers .= "--_e9e06aa5-1550-464d-ace4-e85b575d1899_--" . $newLine;
【问题讨论】:
标签: php email smtp mime email-headers