dtj2ww9500 2018-12-10 15:29
浏览 28
已采纳

附带附件的PHP邮件 - 但文件为空[重复]

This question already has an answer here:

Making a PHP script that also has a text file included as attachment. It works perfectly when I send to a GMAIL-address, but when I send to an address located at another web hoster, I can see that the file is attached, but when I open it, the file is empty (no content)... What can cause this?? This is the code I use:

$filename = basename($dirfilename);
$body = "File is included in mail";

$path = "downloads/";

$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);

$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);

$eol = PHP_EOL;

// Headers
$header = "From: ".$from_name." <".$from_mail.">".$eol;
$header .= "Reply-To: ".$replyto.$eol;
$header .= "MIME-Version: 1.0
";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";

// Message
$message = "--".$uid.$eol;
$message .= "Content-Type: text/html; charset=UTF-8".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= $body.$eol;
$message .= "--".$uid.$eol;
$message .= "Content-Type: text/plain; name=\"".$filename."\"".$eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment;
 filename*=utf-8''" . rawurlencode($filename).$eol;
$message .= $content.$eol;
$message .= "--".$uid."--";

if (mail($mail_to, $subject, $message, $header))
{
    echo "File sent";
} else {
    echo "Fail";
}
</div>
  • 写回答

1条回答 默认 最新

  • doqpm82240 2018-12-10 15:40
    关注

    If you look at a raw email:

    --_004_8b72e1a6b3f242208a89b3fa9240bcf3avode_
    Content-Type: application/pdf; name="TA1_031_21204-2.pdf"
    Content-Description: TA1_031_20181204-2.pdf
    Content-Disposition: attachment; filename="TA1_031_21204-2.pdf";
        size=230847; creation-date="Mon, 10 Dec 2018 11:33:32 GMT";
        modification-date="Mon, 10 Dec 2018 09:44:35 GMT"
    Content-Transfer-Encoding: base64
    
    JVBERi0xLjUNJeLjz9MNCjI4MDYgMCBvYmoNPDwvTGluZWFyaXplZCAxL0wgMjMwODQ3L08gMjgw
    OC9FIDMzOTcvTiAyOC9UIDIzMDE2NS9IIFsgNDY3IDM0NF0+Pg1lbmRvYmoNICAgICAgICAgICAg
    DQoyODEyIDAgb2JqDTw8L0RlY29kZVBhcm1zPDwvQ29sdW1ucyA0L1ByZWRpY3RvciAxMj4+L0Zp
    bHRlci9GbGF0ZURlY29kZS9JRFs8MDU4QTRCNjk5OEFDNzE0M0JCRUZEMDFENjJCOEE5ODg+PDZG
    Q0FDM0U1RTAyOTNDNDU5QzE2MkQzMTczMjM5RTYyPl0vSW5kZXhbMjgwNiAxM10vSW5mbyAyODA1
    

    By the way you are composing your email

    $message .= "Content-Disposition: attachment;
     filename*=utf-8''" . rawurlencode($filename).$eol;
    

    you are adding an unnecessary ' ' and no $eol in front of your file content.

    Try something like:

    $message .= 'Content-Disposition: attachment; filename=". rawurlencode($filename).";'.$eol.$eol;
    

    But this will automatically done by PHPMailer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable