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\PHPMai
ler;
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 2;
$msg = '';
if (array_key_exists('email',
$_POST)) {
$mail->setFrom('quote@xxxx
xxx', 'Quotations');
$mail->addAddress('jimh@xx
xxxx.com',
'Store Copy');
$mail->addCC($_POST['email
'], 'Quote request copy');
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;ch
arset=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'].'</t
d>
<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.amaz
onaws.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\PHPMai
ler;
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.amaz
onaws.com'
;
$mail->Port = 587;
$mail->setFrom('quote@xxxx
xxx', 'Quotations');
$mail->addAddress('jimh@xx
xxxx.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.';
}
}
?>