使用PHPMailer发送电子邮件后无法打开FPDF PDF文件

FPDF PDF file cannot be opened after email using PHPMailer

我正在尝试使用 FPDF 创建一个 PDF 文件并使用 PHPMailer 通过电子邮件发送它。它确实发送了文件,但由于某些原因我无法打开文件,无论是从我的电子邮件还是使用 Adob​​e Reader(这让我觉得文件已损坏)。

这是我的代码

require('fpdf.php');
require 'PHPMailerAutoload.php';
header("Access-Control-Allow-Origin: *");
$pdf = new FPDF();
$pdf->AddPage();

$pdf->SetFont('Arial','UB',28);
$pdf->Cell(0,10,"My Profile",0,1,'C');
$pdf->Ln();

$mail = new PHPMailer();
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'example@gmail.com';                 // SMTP username
$mail->Password = 'password';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
// 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;

// 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."--";

$pdfdoc = $pdf->Output('','S');
$attachment = chunk_split(base64_encode($pdfdoc));
//Set who the message is to be sent from
$mail->setFrom('example@gmail.com', 'Baby Diary');
//Set who the message is to be sent to
$mail->addAddress('example2@gmail.com', 'haha');
//Set the subject line
$mail->Subject = 'PHPMailer mail() test';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AddStringAttachment($attachment, 'doc.pdf', 'base64', 'application/pdf');

if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

当我使用 $pdf->Output() 时它确实给我输出,这提示我问题不应该在 FPDF 端。任何建议将不胜感激。

您的问题出在 phpmailer 而不是 fpdf。尝试使用标准 PHPMailer class 方法。

如果我是你,我会尝试根据 PHPMailer doc samples 中包含的附件​​示例修改你的代码。特别是我会坚持使用 PHPMailer 方法,而不是自己进行 smtp 格式化。

所以首先将您的 pdf 保存到文件,然后尝试使用 $mail->addAttachment() 附加文件。并让 PHPMailer 处理邮件的格式。

所以你最终可能会得到类似于下面这段代码的东西(我没有 运行 它,你可能需要对其进行微调):

require('fpdf.php');
require 'PHPMailerAutoload.php';
header("Access-Control-Allow-Origin: *");
$pdf = new FPDF();
$pdf->AddPage();

$pdf->SetFont('Arial','UB',28);
$pdf->Cell(0,10,"My Profile",0,1,'C');
$pdf->Ln();

$pdfoutputfile = '/some-tmp-dir/temp-file.pdf';
$pdfdoc = $pdf->Output($pdfoutputfile, 'F');

// We're done with the pdf generation
// now onto the email generation

$mail = new PHPMailer();
$mail->isSMTP();                 // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;          // Enable SMTP authentication
$mail->Username = 'example@gmail.com';   // SMTP username
$mail->Password = 'password';            // SMTP password
$mail->SMTPSecure = 'tls';       // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$separator = md5(time());        // a random hash will be necessary to send mixed content

//Set who the message is to be sent from
$mail->setFrom('example@gmail.com', 'From Test address');

//Set who the message is to be sent to
$mail->addAddress('example2@gmail.com', 'to test address');

//Set the subject line
$mail->Subject = 'Test message with attachement';
$mail->Body    = 'Test message with attached files.';

$mail->AddAttachment($pdfoutputfile, 'my-doc.pdf');

if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}