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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?