Avatar of visionetv
visionetv
Flag for United States of America asked on

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.';
    }
}

?>
HTMLPHP

Avatar of undefined
Last Comment
visionetv

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
David Favor

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
visionetv

ASKER
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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck