I am trying to attach a Dompdf file without saving to server via the following code:
The error I get from my PHP response is:
Message could not be sent. Mailer Error: Unknown encoding: pdffilename.pdf
Now, I have tried to add Base64, using $mail->addAttachment
instead of $mail->addStringAttachment
however none of these have worked and I'm constantly getting an error.
I would like the PDF to be attached directly to the email WITHOUT saving to server.
Wondering if anyone could help!
// 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 = '<html>
<body>
<div>
<p>Thank you</p>
</br>
<p><strong>Contact Information</strong></p>
<p>Hey: '.$title.' '.$first_name.' '.$last_name.'</p>
</br>
</br>
<p>Please see the attached document to confirm all your details </p>
</div>
</body>
</html>';
$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;
}
Using Sendgrid's PHP Library
use SendGrid\Mail\Mail;
I have fixed this and successfully attached my DOMPDF file using the below code:
$output = $dompdf->output();
$mail->addAttachment($output,'application/pdf','filename.pdf', 'attachment');
I imagine this would be the same case for using the PHPMailer library too however, Sendgrid PHP Library was exactly what I needed for sending mails using Sendgrid API.
Hope this helps others looking..