Email PDF Attachment with PHP Using FPDF

前端 未结 4 2144
悲哀的现实
悲哀的现实 2020-12-04 10:40

I want to email a PDF as an attachment that was created using FPDF. My code looks like this, but the attachment never comes through.



        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 10:55

    This ended up working for me:

    AddFont('Georgiai','','georgiai.php');
    $pdf->AddPage();
    $pdf->Image('lib/fpdf/image.jpg',0,0,500);
    $pdf->SetFont('georgiai','',16);
    $pdf->Cell(40,10,'Hello World!');
    
    // email stuff (change data below)
    $to = "myemail@example.com"; 
    $from = "me@example.com"; 
    $subject = "send email with pdf attachment"; 
    $message = "

    Please see the attachment.

    "; // 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 = PHP_EOL; // attachment name $filename = "test.pdf"; // encode data (puts attachment in proper format) $pdfdoc = $pdf->Output("", "S"); $attachment = chunk_split(base64_encode($pdfdoc)); // main header $headers = "From: ".$from.$eol; $headers .= "MIME-Version: 1.0".$eol; $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\""; // no more headers after this, we start the body! // $body = "--".$separator.$eol; $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol; $body .= "This is a MIME encoded message.".$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."--"; // send message mail($to, $subject, $body, $headers); ?>

提交回复
热议问题