生成PDF输出并使用phpmailer将其发送到电子邮件

Jun*_*ari 1 php pdf phpmailer

我需要生成pdf输出并将其发送到电子邮件,我收到的电子邮件没有附件我需要生成pdf输出并将其发送到电子邮件,我收到的电子邮件没有附件

require('lib/FPDF/fpdf.php'); 
require 'lib/PHPMailer/PHPMailerAutoload.php';
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'hello india');

$mail = new PHPMailer;
$mail->isSMTP();                                // Set mailer to use SMTP
$mail->Host = SmtpServer;                       // SMTP server
$mail->SMTPAuth = true;                         // Enable SMTP authentication
$mail->Username = SmtpUsername;                 // SMTP username
$mail->Password = SmtpPassword;                 // SMTP password
$mail->SMTPSecure = 'tls';                      // Enable TLS encryption, `ssl` also accepted
$mail->From = FromEmail;
$mail->Port = 587;                              // SMTP Port
$mail->FromName  = 'testing';

$mail->Subject   = $subject;
$mail->Body      = $body;
$mail->AddAddress($emails);
$mail->AddAttachment($pdf->Output("Test Invoice.pdf","F"), '', $encoding = 'base64', $type = 'application/pdf');
return $mail->Send();
Run Code Online (Sandbox Code Playgroud)

Jun*_*ari 5

require('lib/FPDF/fpdf.php'); 
require 'lib/PHPMailer/PHPMailerAutoload.php';
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'hello india');
$pdf->Output("F",'./uploads/OrderDetails.pdf'); 
$mail = new PHPMailer;
$mail->isSMTP();                                // Set mailer to use SMTP
$mail->Host = SmtpServer;                       // SMTP server
$mail->SMTPAuth = true;                         // Enable SMTP authentication
$mail->Username = SmtpUsername;                 // SMTP username
$mail->Password = SmtpPassword;                 // SMTP password
$mail->SMTPSecure = 'tls';                      // Enable TLS encryption, `ssl` also accepted
$mail->From = FromEmail;
$mail->Port = 587;                              // SMTP Port
$mail->FromName  = 'testing';

$mail->Subject   = $subject;
$mail->Body      = $body;
$mail->AddAddress($emails);
$mail->AddAttachment("./uploads/OrderDetails.pdf", '', $encoding = 'base64', $type = 'application/pdf');
return $mail->Send();
Run Code Online (Sandbox Code Playgroud)

如果您不想将文件保存到服务器,则可以像这样直接将pdfoutput发送到电子邮件

    require('lib/FPDF/fpdf.php'); 
    require 'lib/PHPMailer/PHPMailerAutoload.php';
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Write(5,'Hello India');

    $mail = new PHPMailer;
    $mail->isSMTP();                                // Set mailer to use SMTP
    $mail->Host = SmtpServer;                       // SMTP server
    $mail->SMTPAuth = true;                         // Enable SMTP authentication
    $mail->Username = SmtpUsername;                 // SMTP username
    $mail->Password = SmtpPassword;                 // SMTP password
    $mail->SMTPSecure = 'tls';                      // Enable TLS encryption, `ssl` also accepted
    $mail->From = FromEmail;
    $mail->Port = 587;                              // SMTP Port
    $mail->FromName  = 'testing';

    $mail->Subject   = $subject;
    $mail->Body      = $body;
    $mail->AddAddress($emails);
    $mail->addStringAttachment($pdf->Output("S",'OrderDetails.pdf'), 'OrderDetails.pdf', $encoding = 'base64', $type = 'application/pdf');
    return $mail->Send();
Run Code Online (Sandbox Code Playgroud)

  • 这将起作用,但是您可以通过渲染为字符串并直接附加结果来避免保存到本地文件中:`$ mail-> addStringAttachment($ pdf-> Output('S'),'OrderDetails.pdf') ;`。您不需要设置MIME类型。它会从扩展名中自动设置。 (2认同)