使用FPDF向PHP发送PDF附件
发布时间:2020-12-13 16:33:47 所属栏目:PHP教程 来源:网络整理
导读:我想以PDF格式发送一个使用FPDF创建的附件.我的代码看起来像这样,但附件永远不会通过. ?phprequire('lib/fpdf/fpdf.php');$pdf = new FPDF('P','pt',array(500,233));$pdf-AddFont('Georgiai','','georgiai.php');$pdf-AddPage();$pdf-Image('lib/fpdf/giftc
我想以PDF格式发送一个使用FPDF创建的附件.我的代码看起来像这样,但附件永远不会通过.
<?php require('lib/fpdf/fpdf.php'); $pdf = new FPDF('P','pt',array(500,233)); $pdf->AddFont('Georgiai','','georgiai.php'); $pdf->AddPage(); $pdf->Image('lib/fpdf/giftcertificate.jpg',500); $pdf->SetFont('georgiai',16); $pdf->Cell(40,10,'Hello World!'); $doc = $pdf->Output('test.pdf','S'); //define the receiver of the email $to = 'myemail@example.com'; //define the subject of the email $subject = 'Test email with attachment'; //create a boundary string. It must be unique //so we use the MD5 algorithm to generate a random hash $random_hash = md5(date('r',time())); //define the headers we want passed. Note that they are separated with rn $headers = "From: reply@test.comrnReply-To: reply@test.com"; //add boundary string and mime type specification $headers .= "rnContent-Type: multipart/mixed; boundary="PHP-mixed-".$random_hash."""; //read the atachment file contents into a string,//encode it with MIME base64,//and split it into smaller chunks $attachment = chunk_split(base64_encode(file_get_contents($doc))); //define the body of the message. ob_start(); //Turn on output buffering ?> --PHP-mixed-<?php echo $random_hash; ?> Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hello World!!! This is simple text email message. --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <h2>Hello World!</h2> <p>This is something with <b>HTML</b> formatting.</p> --PHP-alt-<?php echo $random_hash; ?>-- --PHP-mixed-<?php echo $random_hash; ?> Content-Type: application/zip; name="attachment.zip" Content-Transfer-Encoding: base64 Content-Disposition: attachment <?php echo $attachment; ?> --PHP-mixed-<?php echo $random_hash; ?>-- <?php //copy current buffer contents into $message variable and delete current output buffer $message = ob_get_clean(); //send the email $mail_sent = @mail( $to,$subject,$message,$headers ); //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" echo $mail_sent ? "Mail sent" : "Mail failed"; ?> 任何熟悉这样做的人我希望使用PHP mail()函数.
这最终为我工作:
<?php require('lib/fpdf/fpdf.php'); $pdf = new FPDF('P','georgiai.php'); $pdf->AddPage(); $pdf->Image('lib/fpdf/image.jpg','Hello World!'); // email stuff (change data below) $to = "myemail@example.com"; $from = "me@example.com"; $subject = "send email with pdf attachment"; $message = "<p>Please see the attachment.</p>"; // 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,$body,$headers); ?> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Codeforces Round #295 (Div. 1) B. Cubes (STL+类拓扑)
- php – Catchable致命错误:类mysqli_stmt的对象无法转换为
- Symfony 2.3无法使用php composer.phar更新删除文件
- zend-framework2 – 使用composer安装组件zf2
- 如何连接常量和变量并使用PHP将其存储在类常量中?
- Linux+Nginx+MySQL下配置论坛程序Discuz的基本教程
- php 在windows下配置虚拟目录的方法介绍
- 一个类可以实例化另一个类吗? (PHP)
- php – Cookies不能在不同的页面上工作
- php – 在保留其他人的同时添加/更改1 GET值的最佳方法?