PHPMailer Sendgrid PHP - DomPDF HTML 渲染附件

问题描述 投票:0回答:1

我试图通过以下代码附加 dompdf 文件而不保存到服务器:

我从 PHP 响应中得到的错误是:消息无法发送。邮件错误:未知编码:pdffilename.pdf

现在,我尝试使用 $mail->addAttachment 而不是 $mail->addStringAttachment 添加 base64,但是这些都不起作用,而且我不断收到错误。

我希望将 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('[email protected]', 'Web Service');
      $mail->addAddress($email);               // This is $_POST['email'];
      $mail->addReplyTo('[email protected]', '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;
  }

`

php phpmailer sendgrid dompdf
1个回答
0
投票

使用 Sendgrid 的 PHP 库

use SendGrid\Mail\Mail;

我已经解决了这个问题并使用以下代码成功附加了我的 DOMPDF 文件:

$output = $dompdf->output();

$mail->addAttachment($output,'application/pdf','filename.pdf', 'attachment');

我想使用 PHPMailer 库也是同样的情况,但是,Sendgrid PHP 库正是我使用 Sendgrid API 发送邮件所需要的。

希望这可以帮助其他人寻找..

© www.soinside.com 2019 - 2024. All rights reserved.