【问题标题】:How do I send a FPDF doc using PHPMailer如何使用 PHPMailer 发送 PDF 文档
【发布时间】:2014-12-14 13:23:28
【问题描述】:

我想合并或链接 FPDF 和 PHPMailer 代码,以便生成文档并通过电子邮件发送。我不想保存文件。我无法找到解决方案。下面是 FPDF 和 PHPMailer 的工作代码。无法将两者合并在一起。

FPDF 网站要求这样做

$mail = new PHPMailer();
...
$doc = $pdf->Output('', 'S');
$mail->AddStringAttachment($doc, 'doc.pdf', 'base64', 'application/pdf');
$mail->Send();

FPDF 代码:

require_once('fpdf/fpdf.php');
$fpdf = new FPDF();
$text="test";

$fpdf->SetMargins(0, 0, 0);
$fpdf->SetAutoPageBreak(true, 0);

define('FPDF_FONTPATH', 'font/');

$fpdf->AddFont('Verdana', '','verdana.php'); // Standard Arial

$fpdf->addPage('L');

$fpdf->Image('images/certificate.jpg', 0, 0, 297, 210);

$fpdf->SetFont('Verdana', '');
$fpdf->SetFontSize(28);
$fpdf->SetTextColor(32, 56, 100); 
$fpdf->SetXY(108, 52); //
$fpdf->Cell(80, 6, $text, 0,0, 'C'); 

$fpdf->Output('Filename.pdf', 'i');

PHPMailer 代码:

require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->IsSMTP();                                // Set mailer to use SMTP
$mail->Host = 'gator3095';                      // Specify main and backup server
$mail->Port = 587;                              // Set the SMTP port
$mail->SMTPAuth = true;                         // Enable SMTP authentication
$mail->Username = 'username';                   // SMTP username
$mail->Password = 'password';                   // SMTP password
$mail->SMTPSecure = 'tls';                      // Enable encryption, 'ssl' also accepted
$mail->From = 'test@hotmail.com.com';
$mail->FromName = 'John Doe';
$mail->AddAddress('recipient@hotmail.com', '');  // Add a recipient
$mail->IsHTML(true);                             // Set email format to HTML

$mail->Subject = 'subject';
$mail->Body    = 'message body';
$mail->AltBody = 'messge body';
$mail->AddAttachment("c:/temp/test.php", "test.php");  

if(!$mail->Send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}
echo 'Message has been sent';

【问题讨论】:

    标签: php phpmailer fpdf


    【解决方案1】:

    $fpdf->Output('Filename.pdf', 'i'); 替换为$fpdf->Output('Filename.pdf', 'S'); 以将您的PDF 保存在硬盘上,而不是将其直接发送到浏览器(如the doc 中所述)。

    然后在 PHPmailer.php 代码的开头或之前调用您的 FPDF 代码生成 PDF 文件,将 $mail->AddAttachment("c:/temp/test.php", "test.php"); 替换为 $mail->AddAttachment("[...the exact place where your file is..]/Filename.pdf", "Filename.pdf");

    最后,在您最后一次回显之前,添加 unlink('Filename.pdf'); 以删除您刚刚发送的临时 PDF 文件。

    【讨论】:

      猜你喜欢
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多