【发布时间】:2023-01-20 18:31:28
【问题描述】:
我正在尝试将包含使用 dompdf 生成的附件的电子邮件发送到收件人列表。使用array_shift() 获取主要电子邮件并将其从数组中删除,并使用foreach 将所有其他电子邮件作为抄送。当我添加 CC 时,电子邮件被发送了两次。如果没有抄送,则只发送一次。即使 CC 为空,也会发送两次。如果我删除那行AddCC(),它将只发送一次。
我可以寻求任何想法或帮助吗?
require_once 'dompdf/autoload.inc.php';
require_once 'phpqrcode/qrlib.php';
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
$recipients = array() // array of emails to be sent
try {
ob_start();
include "pdf.php";
$htmldata = ob_get_clean();
$options = new Dompdf\Options();
$options->getChroot($_SERVER['DOCUMENT_ROOT']);
$options->setisRemoteEnabled(true);
$options->setIsHtml5ParserEnabled(true);
$options->setTempDir('temp'); // temp folder with write permission
$dompdf = new Dompdf\Dompdf($options);
$dompdf->loadHtml($htmldata);
$customPaper = array(0,0,600,600);
$dompdf->set_paper($customPaper);
QRcode::png($data, "qr.png", "L", 4, 4);
$dompdf->render();
$output = $dompdf->output();
//Server settings
$mail->SMTPDebug = 1; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('', '');
$mail->addAddress(array_shift($recipients)); // Add first email as the recipient and remove the email from array
$mail->addReplyTo('');
foreach($recipients as $ccEmail)
{
$mail->AddCC($ccEmail);
}
$mail->addStringAttachment($output, 'attachment.pdf');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Subject'.rand();
$mail->Body = 'Content';
$mail->AltBody = 'content';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
【问题讨论】:
-
良好的代码缩进将帮助我们阅读代码,更重要的是,它将有助于你调试你的代码Take a quick look at a coding standard 为了您自己的利益。您可能会被要求在几周/几个月内修改此代码,最后您会感谢我。
标签: php email duplicates smtp phpmailer