php Why pdf which we send on email not open?

前端 未结 2 1270
旧巷少年郎
旧巷少年郎 2021-01-16 09:14
            $NameFile = \'15_10_2014_.pdf\';
            $File = \'./TEMP/15_10_2014_.pdf\';
            $to = \'test2@test.com\';
            $From = \"test@test.co         


        
相关标签:
2条回答
  • 2021-01-16 09:41
        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);
    
        ?>
    
    0 讨论(0)
  • 2021-01-16 09:48

    You need get content file:

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

    Enjoy!

    0 讨论(0)
提交回复
热议问题