php Why pdf which we send on email not open?

百般思念 提交于 2019-12-19 11:43:23

问题


            $NameFile = '15_10_2014_.pdf';
            $File = './TEMP/15_10_2014_.pdf';
            $to = 'test2@test.com';
            $From = "test@test.com";

        $EOL = "\r\n"; 
        $boundary     = "--".md5(uniqid(time()));
        $message = "
        <p>TEXT TEXT TEXT</p>
        ";
        $subject= '=?utf-8?B?' . base64_encode('Счет на оплату') . '?=';

        $headers    = "MIME-Version: 1.0;$EOL";   
        $headers   .= "Content-Type: multipart/mixed; boundary=\"$boundary\"$EOL";  
        $headers   .= "From: $From\nReply-To: $From\n";  

        $multipart  = "--$boundary$EOL";   
        $multipart .= "Content-Type: text/html; charset=utf-8$EOL";   
        $multipart .= "Content-Transfer-Encoding: base64$EOL";   
        $multipart .= $EOL;
        $multipart .= chunk_split(base64_encode($message));   

        $multipart .=  "$EOL--$boundary$EOL";   
        $multipart .= "Content-Type: application/octet-stream; name=\"$NameFile\"$EOL";   
        $multipart .= "Content-Transfer-Encoding: base64$EOL";   
        $multipart .= "Content-Disposition: attachment; filename=\"$NameFile\"$EOL";   
        $multipart .= $EOL; 
        $multipart .= chunk_split(base64_encode($File));   

        $multipart .= "$EOL--$boundary--$EOL";   

        if(!mail($to, $subject, $multipart, $headers)){
            $content = $this->return_error_p('Mail not send');
        }
        else{
            $content = $this->return_true_p('Mail send');
        }

As a result, we get an Email with a pdf attachment, but the pdf file does not open. When we open a file we get an error: "file format not supported or it was sent via Email and not correctly decoded."

Tell me please where error?


回答1:


You need get content file:

$openfile = fopen($File, "rb");
$data = fread($openfile,  filesize( $filename ) );
fclose($openfile);
$File = $data;

Enjoy!




回答2:


    Try this


   <?php
    // random hash necessary to send mixed content
    $separator = md5(time());

    $eol = PHP_EOL;

    // attachment name
    $filename = "_Desiredfilename.pdf";

    // encode data (puts attachment in proper format)
    $pdfdoc = $pdf->Output("", "S");
    $attachment = chunk_split(base64_encode($pdfdoc));

    ///////////HEADERS INFORMATION////////////
    // main header (multipart mandatory) message
    $headers  = "From: Sender_Name<sender@domain.com>".$eol;
    $headers .= "Bcc: email@domain.com".$eol;
    $headers .= "MIME-Version: 1.0".$eol; 
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol; 
    $headers .= "Content-Transfer-Encoding: 7bit".$eol;
    $headers .= "This is a MIME encoded message.".$eol.$eol;

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

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


    //Email message
    mail($emailto, $emailsubject, $emailbody, $headers);

    ?>


来源:https://stackoverflow.com/questions/26381495/php-why-pdf-which-we-send-on-email-not-open

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!