使用FPDF向PHP发送PDF附件

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了使用FPDF向PHP发送PDF附件脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我想以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 \r\n
$headers = "From: reply@test.com\r\nReply-to: reply@test.com";
//add boundary string and mime tyPE specification
$headers .= "\r\nContent-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);

?>

脚本宝典总结

以上是脚本宝典为你收集整理的使用FPDF向PHP发送PDF附件全部内容,希望文章能够帮你解决使用FPDF向PHP发送PDF附件所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。