【发布时间】:2015-01-27 23:18:07
【问题描述】:
**更新:更新了代码并添加了在 Gmail 中看到的电子邮件内容。 **
我有一个问题,我已经尝试解决了几天。但由于我一直无法取得任何进展,我希望得到其他人的帮助。我正在尝试将 zip 文件从我的服务器发送到我的电子邮件。但是,电子邮件是空的。这是代码:
`$headers = "From: $from";
/* Generate boundary string */
$random = md5(time());
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random."\"";
/* Read the file */
$attachment = chunk_split(base64_encode(file_get_contents("backup-$date-$time-9.zip")));
/* Define body */
$message = "--PHP-mixed-$random
Content-Type: text/plain; charset=\"iso-8859-1\"
Attached, please find your backup file.
--PHP-mixed-$random
Content-Type: application/zip; name=backup-$date-$time-9.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random--"; // no intendation
$mail = mail($email, $subject, $message, $headers);
echo $mail ? "Email sent<br>" : "Email sending failed<br>";`
感谢所有阅读并希望提供帮助的人。
在 Gmail 中看到的结果电子邮件是
附件,请找到您的备份文件。
--PHP-mixed-e44b531b0e0538185289abc521eda78d
Content-Type: application/zip; name=backup-29-11-2014-1417275285-9.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment
UEsDBBQAAAAIAFZ8fUUs...sUEsFBgAAAAACAAIAeAAAAD4B
AAAAAA==
--PHP-mixed-e44b531b0e0538185289abc521eda78d--
【问题讨论】:
-
我没有发现错误,你检查过所有变量是否都被正确填充了吗?即 echo $headers, echo $random, echo $message ...
-
$headers 是
From : <email> Content-Type: multipart/mixed; boundary:"PHP-mixed-ed0c7bd6c7024332067e17602743495e"。其余的也很好。 -
好的,我浏览了各种电子邮件源代码和一些教程,我在您的标题中注意到了这一点:
boundary: PHP-mixed-$random- 尝试使用=而不是:,就像这样:boundary = PHP-mixed-$random -
@chrki 我这样做了,但现在所有电子邮件都被视为文本。例如,附件是正文的一部分(作为文本)。
标签: php