Search code examples
phpphpmailer

I have converted HTML to PDF but struggling to send the PDF with PHPmailer


I have managed to generate pdf from my HTML page but unable to send the pdf as an attachment using phpmailer. Please, see my code below. What am I missing?

Key points:

  1. The html(tractpage2.php) renders well to PDF
  2. PHPmailer works but it only sends the $message without the attachment.
  3. Problem is that the pdf does not attach the mail

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
    
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'PHPMailer/src/Exception.php';


//reference the dompdf namescape

use Dompdf\Dompdf;

//initialise dompdf class


// get/setup the htmlpage

ob_start();
require("tractpage2.php"); // The html document
$page = ob_get_contents();
ob_end_clean();

// convert to pdf

$document = new Dompdf();
$document->set_option('defaultFont', 'Courier');
$document->load_html($page);

// set paper orientation

$document->set_paper('A4', 'portrait');

// Render the HTML as PDF
$document->render();

// Output the generated PDF to Browser
$document->stream("contract.pdf", array("Attachment"=>0));

//1 = download
//0= preview

$fileupload = $document->output();

// setup email 

$message = "Am new to programming and loving it";



 $mail = new PHPMailer;                             

    //Server settings
    $mail->SMTPDebug = 2;                                 // 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 = '[email protected]';                 // SMTP username
    $mail->Password = 'xxxxxx';                           // SMTP password
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('[email protected]', 'james');
    $mail->addAddress('[email protected]', 'name of receiver');     // Add a recipient
    $mail->addAddress('[email protected]');               // Name is optional
 
    //Attachments
   $mail->addAttachment($fileupload);         // Add attachments
 
 

    //Content

    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = $message;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

   if ($mail->send()){
    echo 'Message has been sent';}
else { echo 'Message could not be sent.';}
    
   



?>


Solution

  • You would want to create your file on server and save and then pass absolute path of pdf file to addAttachment or alternatively you can also directly attach the DomPDF output without creating a file, like so:

    $mail->addAttachment($fileupload,'application/pdf','output.pdf', false);