У меня есть 3 раздела кода, как показано ниже.
<?php
include_once '.././connect/init.php';
global $db;
if (isset($_GET['success']) && empty($_GET['success'])) {
$registerd_succ = '<p class="success2" style="width: 100%">' . ' Mail with invoice sent successfully.' . '</p>';
echo $registerd_succ;
}
if (isset($_GET['failure']) && empty($_GET['failure'])) {
$registerd_succ = '<p class="success2" style="width: 100%">' . 'Failed to send Mail with invoice.' . '</p>';
echo $registerd_succ;
}
//include autoloader
require_once '.././dompdf/dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
use Dompdf\Options;
//initialize dompdf class
$options = new Options();
$options->setIsRemoteEnabled(true);
$document = new Dompdf($options);
$contxt = stream_context_create([
'ssl' => [
'verify_peer' => FALSE,
'verify_peer_name' => FALSE,
'allow_self_signed'=> TRUE
]
]);
$document->setHttpContext($contxt);
// section 1 code Generate PDF for Invoice
$output = 'My invoice HTML';
$document->loadHtml($output);
$document->setPaper('A4', 'potrait');
$document->render();
$invhead = 'Invoice-No-' . $inv_no . '.pdf';
$invhead1 = 'Invoice-No-' . $inv_no;
$dir_to_save = "../invoices/";
// Save file to server folder
$output1 = $document->output(['isRemoteEnabled' => true]);
file_put_contents($dir_to_save . $invhead, $output1);
// $document->stream("$invhead1", array("Attachment" => 0));
//1 = Download
//0 = Preview
// Section 2 code Generate PDF for packing list
$document_pl = new Dompdf();
$output_pl = 'Packing list content';
$document_pl->loadHtml($output_pl);
$document_pl->setPaper('A4', 'potrait');
$document_pl->render();
$packlist = 'Packing List-No-' . $id . '.pdf';
$packlist1 = 'Packing List-No-' . $id;
$dir_to_save = "../invoices/";
// Save file to server folder
$output2 = $document_pl->output();
file_put_contents($dir_to_save . $packlist, $output2);
// $document_pl->stream("$packlist1", array("Attachment" => 0));
//1 = Download
//0 = Preview
// Section 3 code - Sending mail with attachment of PDF files with PhpMailer
// SEND EMAIL WITH ATTACHMENTS
$email = 'kpkdhar22@gmail.com';
$name = 'Poorna Kaladhar';
$phone = '9951554741';
$message = 'Sample Message';
require("./PHPMailer/src/PHPMailer.php");
require("./PHPMailer/src/SMTP.php");
require("./PHPMailer/src/Exception.php");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$file_name = $invhead;
$file_name1 = $packlist;
$mail->Username = "kpkdhar22@gmail.com"; // SMTP username
$mail->Password = "XXXXXXXX"; // SMTP password
$mail->addAttachment("invoices/" . $invhead);
// $mail->addAttachment("../invoices/" . $file_name1);
$mail->From = "kpkdhar22@gmail.com";
$mail->FromName = "XXXXXX";
$mail->SMTPSecure = 'ssl';
$mail->Port = 465; //SMTP port
$mail->addAddress("kpkdhar22@gmail.com", "Poorna xxxxxx");
$mail->Subject = "Your invoice from xxxxxxxxx";
$mail->Body = "
Name: $name<br>
Email: $email<br>
Telephone: $phone<br><br><br>
Comments: $message";
$mail->AltBody = $message;
if (!$mail->Send()) {
header('location:send_inv_packlist1.php?success');
exit();
} else {
header('location:send_inv_packlist1.php?failure');
exit();
}
Когда я запускаю код с 1,2,3, запускается только код раздела 1, тогда в браузере отображается pdf.почему второй pdf не генерируется, при отладке мой курсор запускается до $ document_pl-> render ();затем выходит.Когда я запускаю код с 1,3 или 2,3, все еще сгенерированный PDF отображается в браузере, и почта отправляется с вложением.но оно не перенаправляется на сообщение об успехе или неудаче.Просто PDF отображается в браузере.Я хочу, чтобы оба PDF-файла были сгенерированы и сохранены в каталоге и отправлены на почту в виде вложений.Я могу отправить несколько вложений с существующими файлами, но не по приведенному выше сценарию.
Любой совет или полезное руководство приветствуются.