Я пытаюсь прикрепить файл dompdf без сохранения на сервер с помощью следующего кода:
В ответ PHP я получаю следующую ошибку: Не удалось отправить сообщение. Ошибка почтовой программы: неизвестная кодировка: pdffilename.pdf
Теперь я попытался добавить base64, используя $mail->addAttachment вместо $mail->addStringAttachment, однако ни один из них не сработал и Я постоянно получаю сообщение об ошибке.
Я хочу, чтобы PDF-файл был прикреплен непосредственно к электронному письму БЕЗ сохранения на сервере.
Интересно, есть ли у кого-нибудь может помочь!
Код: Выделить всё
// Required Libraries
require __DIR__ . "/vendor/autoload.php";
require __DIR__ . "/vendor/phpmailer/phpmailer/src/PHPMailer.php";
require __DIR__ . "/vendor/phpmailer/phpmailer/src/Exception.php";
require __DIR__ . "/vendor/rodrigoq/phpmailersendgrid/src/PHPMailerSendGrid.php";
// Use Classes from Libs
use Dompdf\Dompdf;
use Dompdf\Options;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailerSendGrid;
// PDF Gen
$options = new Options;
$options->setChroot(__DIR__);
$options->setIsRemoteEnabled(true);
$dompdf = new Dompdf($options);
$dompdf->setPaper("A4","landscape");
$html = file_get_contents("pdftemplate.html");
// HTML STR_REPLACE to insert POST values into the html file above
$html = str_replace(["{{ date }}", "{{ title }}", "{{ first_name }}", "{{ last_name }}", " {{ email }}", "{{ phone }}", "{{ address_line_1 }}", "{{ address_line_2 }}", "{{ city }}", "{{ county }}", "{{ postcode }}"],
[$date, $title, $first_name, $last_name, $email, $phone, $address_line_1, $address_line_2, $city, $county, $postcode], $html);
$dompdf->loadHTML($html);
$dompdf->render();
$dompdf->addInfo("Title", "PDF File Title");
$attachthispdf = $dompdf->output();
// Now that the PDF is created and ready (I have tested, it generates perfectly fine when saving to server) - I attempt to attach $attachthispdf to my Mail function below
$mail = new PHPMailerSendGrid(true);
try {
$mail->isSendGrid();
$mail->SendGridApiKey = 'MYSENDGRID_API_KEY'; // SendGrid API Key.
//Recipients
$mail->setFrom('service@mydomain.com', 'Web Service');
$mail->addAddress($email); // This is $_POST['email'];
$mail->addReplyTo('contact@mydomain.com', 'Web Service');
//Attachments
$mail->addStringAttachment($quotepdf, 'application/pdf', 'pdffilename.pdf'); // PDF Docu attachment
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'We have received your request';
$mail->Body = '
Thank you
[b]Contact Information[/b]
Hey: '.$title.' '.$first_name.' '.$last_name.'
Please see the attached document to confirm all your details
';
$mail->send();
echo 'Message has been sent.';
header("Location: http://example.com/thank-you.html"); // I would like to redirect to this specific page upon success
exit();
}
catch (Exception $e)
{
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
Источник: https://stackoverflow.com/questions/781 ... tml-render
Мобильная версия