'Output ('',S); breaking code with Mpdf and PHPmailer

I am trying to set up sending a pdf attachment with mPDF and PHPMailer. mPDF is woking fine when output is download to browser, the minute I change it to Output('',S) the code stops working with Error 500 and I have tried different versions of the mailer code with no luck. I cant find any error with the syntax and all online tutorials seem to agree with the code. I am stumped

My code:

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once __DIR__ . '/vendor/autoload.php';


// Grab variables
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$message = $_POST['message'];

$mpdf = new \Mpdf\Mpdf();

$data = "";
$data .= "<h1>Testing this</h1>";

//Add Data
$data .='<strong>First Name</strong> '. $fname.'<br>';
$data .='<strong>Last Name</strong> '. $lname.'<br>';
$data .='<strong>Email</strong> '. $email .'<br>';

if($message)
{
    $data .='<br/><strong>Message</strong><br/> '. $message.'<br>';
}

//WritePdf
$mpdf->WriteHtml($data);

//output to bowser
$pdf = $mpdf->output('',S);

$enquirydata = [
'First Name' => $fname,
'Last Name' => $lname,
'Email' => $email,
'Message' => $message
];

sendEmail($pdf,$enquirydata);

function sendEmail($pdf, $enquirydata)
{

$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'in-v3.mailjet.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'username';                     // SMTP username
    $mail->Password   = 'password';                               // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('[email protected]', 'Test Form');
    $mail->addAddress('[email protected]', 'Joe User');     // Add a recipient
    $mail->addReplyTo('[email protected]', 'Information');
    $mail->addCC('[email protected]');
    $mail->addBCC('[email protected]');

    // Attachments
 
    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $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}";

}
}

?>


Solution 1:[1]

If you are in a localhost, comment the line with the validation $email->isSMTP(); It works for me in my local.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vannia Aquino