使用 FPDF 通过 PHP 发送 PDF 附件

Posted

技术标签:

【中文标题】使用 FPDF 通过 PHP 发送 PDF 附件【英文标题】:Email PDF Attachment with PHP Using FPDF 【发布时间】:2011-05-20 04:34:50 【问题描述】:

我想通过电子邮件将使用 FPDF 创建的 PDF 作为附件发送。我的代码看起来像这样,但附件永远不会通过。

<?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',0,0,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() 函数。

【问题讨论】:

如果不是出于学习目的,请使用 Swiftmailer 之类的 PHP 邮件程序库。 swiftmailer.org 正如 Pekka 所说,Swift Mailer 比手动处理带有附件的邮件要好得多。 【参考方案1】:

我认为你有一个多余的命令。您正在使用 Output() 命令的 string 变体:

$doc = $pdf->Output('test.pdf', 'S');

那么你正在对它执行file_get_contents()

$attachment = chunk_split(base64_encode(file_get_contents($doc)));

它不是文件,它是字符串中的文件,因为如果 $doc 是文件名,file_get_contents() 将返回。

只需将其缩减为:

$attachment = chunk_split(base64_encode($doc));

然后看看是否还有错误发生。

【讨论】:

我没有再收到任何错误,但该文件从未附加。我发送的电子邮件有一个 0kb 的附件“noname”。关于为什么会这样的任何想法?感谢您的帮助。 手动使 MIME 编码工作非常困难,尤其是当您添加复杂的附件时,我绝对建议您在问题中提及使用邮件库。顺便说一句,您似乎告诉它文件名是附件.zip,它是一个 zip 文件,您从来没有压缩过 PDF。【参考方案2】:

这最终对我有用:

<?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/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 = "<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, $subject, $body, $headers);

?>

【讨论】:

谢谢,在 Gmail 中完美运行,但在 Outlook 中,电子邮件是空白页,邮件是第二个附件...您知道是什么原因造成的吗? 我正在使用此代码,但有时并非所有收件人都收到了一些电子邮件。但是我设置的密件抄送似乎总是能通过。我的代码在 Go Daddy 共享主机(豪华)上运行...有人知道为什么吗?【参考方案3】:

如果你使用 PHPMailer

$attachment= $pdf->Output('attachment.pdf', 'S');

$mailer->AddStringAttachment($attachment, 'attachment.pdf');

享受

【讨论】:

你拯救了我的一天。谢谢。在看到您的代码之前,我尝试了 base64_encode 和 chunk_split 方法来添加附件。只需清除这些方法,一切就完美了。 这里也一样。奇迹般有效。谢谢 几个小时的搜索,这是很好的答案!【参考方案4】:
Create one pdf file name as testing.pdf  
 <?php 
    //define the receiver of the email 
    $to = 'elangovan2me@gmail.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: elangovan2me@gmail.com\r\nReply-To: meeetcity@gmail.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('testing.pdf'))); 

    //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>Email Pdf File Attachements</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/pdf; name="attachment.pdf"  
    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"; 
?>

【讨论】:

以上是关于使用 FPDF 通过 PHP 发送 PDF 附件的主要内容,如果未能解决你的问题,请参考以下文章

在 mime smtp 中添加 pdf 的附件

FPDF 错误:一些数据已经输出,无法发送 PDF 文件。尝试了一切,但没有任何帮助

如何使用php和fpdf创建发票pdf? [关闭]

FPDF 错误:一些数据已经输出,无法发送 PDF

PHP使用FPDF pdf添加水印中文乱码问题

使用 PHP Mail() 发送附件?