Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在电子邮件功能中发送附件,但无法获取附件_Php_Email - Fatal编程技术网

Php 在电子邮件功能中发送附件,但无法获取附件

Php 在电子邮件功能中发送附件,但无法获取附件,php,email,Php,Email,我正在使用此功能在邮件中附加pdf。它工作正常,但当用户试图打开pdf文件时,他会收到一条消息,告诉他由于文件格式问题,无法打开该文件 // a random hash will be necessary to send mixed content $separator = md5(time()); // carriage return type (we use a PHP end of line constant) $eol = "\r\n";

我正在使用此功能在邮件中附加pdf。它工作正常,但当用户试图打开pdf文件时,他会收到一条消息,告诉他由于文件格式问题,无法打开该文件

// a random hash will be necessary to send mixed content
        $separator = md5(time());

        // carriage return type (we use a PHP end of line constant)
        $eol = "\r\n";

        // attachment name
        $filename = $invoice.'.pdf';

        // encode data (puts attachment in proper format)
        $attachment = chunk_split(base64_encode($filename));
        //$attachment = $filename;

        // main header
        $from     = "test@productionserver.in";
        $headers  = "From: ".$from.$eol;
        $headers .= "MIME-Version: 1.0".$eol; 
        $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol;

        // no more headers after this, we start the body! //
        $body = "--".$separator.$eol;
        $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;


        // message
        $body .= "--".$separator.$eol;
        $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
        $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
        $body .= $message.$eol;

        // attachment
        $body .= "--".$separator.$eol;
        $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
        $body .= "Content-Transfer-Encoding: base64".$eol;
        $body .= "Content-Disposition: attachment".$eol.$eol;
        $body .= $attachment.$eol;
        $body .= "--".$separator."--";

mail($to,$subject,$body,$headers);
有人知道我做错了什么吗

$attachment = chunk_split(base64_encode($filename));
您正在将文件名编码为附件。您需要对实际的文件进行编码,而不仅仅是对其名称进行编码

$attachment = chunk_split(base64_encode(file_get_contents($filename)));

下载PDF文件并在文本编辑器(如记事本)中打开它。看看里面有没有什么东西。另外,我不建议像你那样手动创建电子邮件。我建议您使用一个预先制作的库来完成这项工作。只有文件名没有其他内容我如何使用该库?@Rocket HazmatTo完成@RocketHazmat的建议,我向您推荐。