我以前用过DomPDF和PHPOffice库,它工作得很好,但现在我试图也发送PDF作为电子邮件,我有吨的问题,让它工作。它给我的错误未捕获错误:调用未定义的方法Dompdf\Options::setPaper(),我不确定原因
下面是我正在使用的代码。getMemberDonations($year)函数只返回一个对象/数组,其中包含一堆成员细节。有人知道如何解决这个问题吗?
require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Dompdf;
use Dompdf\Options;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Get year from form data
$year = $_POST['year'];
// Get member donation data for the given year
$members = getMemberDonations($year);
// Configure SMTP settings for sending email
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'liccftest@gmail.com';
$mail->Password = 'citgxpzhmtoticrv';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
// Prepare email body
$body = 'Dear {first} {last},<br><br>' .
'Thank you for your donation of ${amount} to LICCFTaxReceipt in {year}.<br>' .
'Please find attached your tax receipt for this donation.<br><br>' .
'Sincerely,<br>LICCFTaxReceipt Team';
// Iterate over member donation data
foreach ($members as $member) {
$variables = [
'{first}' => $member['f_name'],
'{last}' => $member['l_name'],
'{address}' => $member['address'],
'{city}' => $member['city'],
'{province}' => $member['province'],
'{postal}' => $member['postal_code'],
'{amount}' => $member['total_amount'],
'{year}' => $year,
'{email}' => $member['email'],
];
$spreadsheet = IOFactory::load('receipt template.xlsx');
$worksheet = $spreadsheet->getActiveSheet();
// Replace variables in spreadsheet with member data
foreach ($worksheet->getRowIterator() as $row) {
foreach ($row->getCellIterator() as $cell) {
$value = $cell->getValue();
foreach ($variables as $variable => $replacement) {
$value = str_replace($variable, $replacement, $value);
}
$cell->setValue($value);
}
}
// Set active sheet
$spreadsheet->setActiveSheetIndex(0);
$options = new Options();
$options->setPaper('letter', 'portrait');
$writer = new Dompdf($spreadsheet, $options);
// Generate PDF content from spreadsheet data
$writer->loadHtml($spreadsheet->getActiveSheet()->getDirtyData());
$writer->render();
$pdfContent = $writer->output();
// Save PDF file to disk
$fileName = "LICCFTaxReceipt{$year}.pdf";
file_put_contents($fileName, $pdfContent);
// Attach PDF file to email
$mail->addStringAttachment($pdfContent, $fileName);
// Set email details
$mail->setFrom('liccftest@gmail.com', 'LICCF');
$mail->addAddress($member['email']);
$mail->Subject = 'Tax Receipt for your donation to LICCFTaxReceipt for the year of ' . $year;
$mail->Body = str_replace(array_keys($variables), array_values($variables), $body);
// Send the email
if($mail->send()) {
echo 'Email sent successfully to ' . $to . '<br>';
} else {
echo 'An error occurred while sending the email to ' . $to . ': ' . $mail->ErrorInfo . '<br>';
}
}
1条答案
按热度按时间ql3eal8s1#
所以我决定暂时将文件保存到服务器,然后读取文件并将其传递给addStringAttachment()函数。这似乎解决了我的问题,因为我不需要使用setPaper或类似的东西。