【问题标题】:FPDF PDF file cannot be opened after email using PHPMailer使用 PHPMailer 发送电子邮件后无法打开 FPDF PDF 文件
【发布时间】:2015-01-19 08:25:28
【问题描述】:

我正在尝试使用 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 方面。任何建议将不胜感激。

【问题讨论】:

    标签: email pdf attachment phpmailer fpdf


    【解决方案1】:

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

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

    所以首先将您的 pdf 保存到文件中,然后尝试使用 $mail-&gt;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!";
    }
    

    【讨论】:

    • 哇,原来如此!此外,我可以同时存储到我的服务器中!
    猜你喜欢
    • 2015-09-06
    • 2020-11-25
    • 2019-01-19
    • 2023-03-20
    • 2017-07-19
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    相关资源
    最近更新 更多