【发布时间】:2021-06-27 04:28:40
【问题描述】:
我正在使用 FPDF 和 Phpmailer 生成 PDF 文件并将其作为电子邮件附件发送。
当我将它们用作独立脚本时,我的 PDF 生成脚本和 phpmailer 运行良好。
现在,当我结合这两个脚本来生成和显示 PDF 表单(不将其保存到文件系统)并将此 PDF 文档作为附件发送时,它无法生成 PDF 并将其显示在浏览器上,但不通过邮件发送。
我的代码是:
<?php declare(strict_types=1);
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$pdf = new mypdf();
$pdf->AliasNbPages();
$pdf->AddPage('P', 'A4', 0);
$pdf->Header();
$pdf->headerTable();
$pdf->viewTable();
$pdf->footer();
$pdf->setMargins(20, 20, 20);
$pdf->output();
$pdfString = $pdf->output();
$mail = new PHPMailer;
// getting post values
$first_name = $_POST['fname'];
$to_email = "receipent@gmail.com";
$subject = "stationery issued";
$message = "Dear sir your requested stationery has been issued by Stationery store ";
$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 = 'sender@gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->setFrom('sender@gmail.com', 'Your_Name');
$mail->addReplyTo('sender@gmail.com', 'Your_Name');
$mail->addAddress($to_email); // Add a recipient
$mail->addStringAttachment((string)$pdfString, 'name file.pdf');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = 'Dear ' . $first_name . '<p>' . $message . '</p>';
$mail->send();
【问题讨论】: