如何将dompdf PDF发送到电子邮件附件而不将PDF保存到任何文件夹?

时间:2018-10-08 08:50:22

标签: php phpmailer email-attachments

是否可以将dompdf生成的PDF发送到电子邮件附件,而无需将PDF保存在文件夹中?我不想将此生成的PDF保存到任何文件夹。而不是将生成的PDF直接发送到电子邮件发送?

这是我使用Dompdf包含电子邮件附件的发送电子邮件php代码。如何在其中附加PDF?

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use Dompdf\Dompdf;
use Dompdf\Options;

//Load Composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
$getcusemail     = $_POST['getcusemail'];
$getcustomername = $_POST['getcustomername'];
$getinvoiceNo = $_POST['getinvoiceNo'];
$getinvoiceDate = $_POST['getinvoiceDate'];
$getbilltot = $_POST['getbilltot'];
$getadvamt = $_POST['getadvamt'];
$getbalamt = $_POST['getbalamt'];
$html =
  '<html><body>'.
  '<p>Put your html here, or generate it with your favourite '.
  'templating system.</p>'.
  '</body></html>';

  // instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->set_option('defaultFont', 'Courier');
$dompdf->loadHtml($html);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();
$output = $dompdf->output();
// Output the generated PDF to Browser
//$output1  = $dompdf->stream("invoice",$output);

try {
    //Server settings
    $mail->SMTPDebug = 0;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'XXX456@gmail.com';                 // SMTP username
    $mail->Password = 'XXXXXX';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('xxx456@gmail.com', 'Invoice');
    $mail->addAddress('xxx123@gmail.com', 'Name');     // Add a recipient

    //Attachments
    $mail->addAttachment("coreworld",$output);         // Add attachments

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Invoice #'.$getinvoiceNo.' from xx LLC';
    $mail->Body    = 'Dear '.$getcustomername.',<br><br>'
                    .'Please find your attached invoice.We appreciate your prompt payment.<br><br>'
                    .'Invoice Number: '.$getinvoiceNo.'<br>'
                    .'Invoice Date: '.$getinvoiceDate.'<br>'
                    .'Advance Amount: '.$getadvamt.'<br>'
                    .'Balance Amount: '.$getbalamt.'<br>'
                    .'Bill Total: '.$getbilltot.'<br><br>'
                    .'Sincerely<br>'
                    .'xxx, LLC';

    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

0 个答案:

没有答案