【问题标题】:Empty message when sending zip file with PHP使用 PHP 发送 zip 文件时出现空消息
【发布时间】: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 : &lt;email&gt; Content-Type: multipart/mixed; boundary:"PHP-mixed-ed0c7bd6c7024332067e17602743495e"。其余的也很好。
  • 好的,我浏览了各种电子邮件源代码和一些教程,我在您的标题中注意到了这一点:boundary: PHP-mixed-$random - 尝试使用= 而不是:,就像这样:boundary = PHP-mixed-$random
  • @chrki 我这样做了,但现在所有电子邮件都被视为文本。例如,附件是正文的一部分(作为文本)。

标签: php


【解决方案1】:

我通过进行以下更改使其正常工作:

  • 删除From: 标头中的空格字符,否则为无效标头
  • boundary: 更改为boundary=
  • 删除了$message 中的意图,因为这似乎破坏了电子邮件。边界字符串--PHP-mixed-random 将电子邮件的不同部分分隔开,例如纯文本、html 文本、附件。当边界分隔符前面有空格时,不再解释为分隔符,而是普通文本。

代码如下:

<?php
$from = "xxx@myserver";
$email = "xxx@gmail.com";
$subject = "backup file";
$date = "29-11-2014";
$time = "1417275285"; // test file "backup-29-11-2014-1417275285-9.zip" in the same folder

$headers = "From: $from"; // remove space
/* Generate boundary string */
$random = md5(time());
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random."\""; // : to =
/* 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 和 Mozilla Thunderbird 中显示得非常好
  • 包括换行符和空格?
  • 是的,我只是复制并粘贴了我的代码,电子邮件看起来不错:截图i.imgur.com/KSUZn8a.png
  • 我添加了 2 句话来解释发生了什么。这是我发现的关于在 PHP 代码中放置长字符串的线程,它显示了几种可能性:stackoverflow.com/questions/1848945/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 2012-09-03
  • 2016-12-22
  • 1970-01-01
  • 2020-12-05
相关资源
最近更新 更多