troubleshooting Question

PHPMailer Error "Message body empty"

Avatar of visionetv
visionetvFlag for United States of America asked on
HTMLPHP
2 Comments1 Solution3470 ViewsLast Modified:
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.';
    }
}

?>
ASKER CERTIFIED SOLUTION
David Favor
Fractional CTO
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros