PHPMailer Error "Message body empty"

Hello Experts;

I am passing form variables to a script that sends a response Email from an AWS ec2 instance using the SMTP interface. I have no problem sending a simple phpmailer text response Email but I cannot send an HTML formatted Email. I receive the following browser message when tyring to send an HTML Email: "Mailer Error: Message body empty".

I can echo the variables inserted into the HTML but the code will not finish sending the Email. I've searched the web for hours trying to find a solution but none seem to align with the issue in my script; hopefully someone can point out my error.

My code for the "HTML phpmailer" that causes the error is:

<?php
session_start();
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 2;
$msg = '';

if (array_key_exists('email', $_POST)) {
    $mail->setFrom('quote@xxxxxxx', 'Quotations');
    $mail->addAddress('jimh@xxxxxx.com', 'Store Copy');
    $mail->addCC($_POST['email'], 'Quote request copy');
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= "X-Mailer: PHP/" . phpversion();
    $headers .= "X-Priority: 1" . "\r\n";
    $mail->addReplyTo($_POST['email'], $_POST['name']);
    $mail->Subject = 'Quote Request';
    $mail->isHTML(true);
            
    $message = '
    <html>
    <head>
    <title>Quote Request</title>
    </head>
    <body>
    <table>
    <tr>
    <td>'.$_POST['email'].'</td>
    <td>'.$_POST['telephone'].'</td>
    <td>'.$_POST['list'].'</td>
    <td>'.$_POST['comments'].'</td>
    </tr>
    </table>
   </body>
   </html>
   ';
   echo $message;
   
   $mail->isSMTP();
   $mail->SMTPAuth = true;

    $mail->Username = 'XXXXXX';
    $mail->Password = 'XXXXXXXXXXX';
    $mail->Host = 'email-smtp.us-west-2.amazonaws.com';
    $mail->Port = 587;

        if ($mail->send()) {
            echo 'Your mail has been sent successfully.';
                  session_unset();
            session_destroy();
                  echo $_POST['email'];      
                  echo "<meta http-equiv='refresh' content='0;url=http://XX.XXX.XXX.XXX/exit.htm'>";
        } else {
                  echo 'Unable to send email. Please try again.';
                  echo "Mailer Error: " . $mail->ErrorInfo;                  
       }
    } else {
        //$msg = 'Invalid email address, message ignored.';
  }

?>

My code for the "Text phpmailer" which works fine is:

<?php
session_start();
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 2;
$msg = '';
//Don't run this unless we're handling a form submission
if (array_key_exists('email', $_POST)) {
    date_default_timezone_set('Etc/UTC');
    $mail->isSMTP();
    $mail->SMTPAuth = true;
    $mail->Username = 'XXXXXXX';
    $mail->Password = 'XXXXXXXXXXXX';
    $mail->Host = 'email-smtp.us-west-2.amazonaws.com';
    $mail->Port = 587;

     $mail->setFrom('quote@xxxxxxx', 'Quotations');
    $mail->addAddress('jimh@xxxxxx.com', 'Store Copy');
    $mail->addCC($_POST['email'], 'Quote request copy');
   
    if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
        $mail->Subject = 'Quote Request';
        $mail->isHTML(false);
       
        $mail->Body = <<<EOT
        From: {$_POST['email']}
        Name: {$_POST['name']}
        Phone: {$_POST['telephone']}
        List: {$_POST['list']}
       Comments: {$_POST['comments']}
EOT;
        //Send the message, check for errors
        if (!$mail->send()) {
            $msg = 'Sorry, something went wrong. Please try again later.';
        } else {
            $msg = 'Message sent! Thanks for contacting us.';
                  session_unset();
            session_destroy();       
                  echo "<meta http-equiv='refresh' content='0;url=http://xx.xxx.xxx.xxx/exit.htm'>";
        }
    } else {
        $msg = 'Invalid email address, message ignored.';
    }
}

?>
visionetvAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Trick to use.

Rather than trying to send the message, write the message to a file, so you can debug your format.

Another trick to use.

Split out your session management code + mail code, so you can run your mail code on the command line as a PHP script for easier debugging.

Read https://github.com/PHPMailer/PHPMailer/wiki/Tutorial carefully (HTML Section).

One thing which seems missing is actually flagging the message as HTML.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
visionetvAuthor Commented:
Thanks for directing me to GitHub, I been there before but somehow missed the barebones PHP HTML example and now my script is now working. Still have some HTML <table> formatting to work through though that should be pretty straightforward. - Thanks again
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.