Jenarroyo Jenarroyo - 3 months ago 24
PHP Question

Using AWS SES, how to make the email content to HTML?

Here is my code, for some reasons.. it is working perfectly fine in my localhost and it can send email with HTML content and is rendered perfectly. When I upload my codes on the production, the email seems to be in text format and all the HTML tags are shown. What to do with this kind of issue?

There are no errors and email is sent (based from the error info on the last part of the code), its just the rendering of the BODY Im having trouble with. Thanks!

<?php

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');

require '../phpmailer/PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer();

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging

$mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'email-smtp.us-west-2.amazonaws.com';

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
// I tried PORT 25, 465 too
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

// Username to use for SMTP authentication - use full email address for gmail
$mail-> Username = "USERNAME";

// Password to use for SMTP authentication
$mail-> Password = "PASSWORD";

//Set who the message is to be sent from
$mail->setFrom('from@email.com', '');

//Set who the message is to be sent to
$mail->addAddress('example@email.com', '');



//Set the subject line
$mail->Subject = 'TEST Mail';

//this is just an example content
$mail->Body = '
<div style="background-color:#254a64;">
<div align="center" style="color:white; padding:20px 0px 10px 5px; font-size: 1.5em; font-family:Arial;">
SAMPLE HEADER
</div>
</div>
<div style="background-color: #e8c020; padding: 2px;"></div>
<br><br>';

//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body ALT BODYYYY';

//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>


This question in now closed. Thank you! You may check the answer from below.

Answer

Calling the isHTML() method after the $mail->Body has been set solved the problem for me:

$mail->Body    = $Body;
$mail->IsHTML(true);       // <=== call IsHTML() after $mail->Body has been set.

Issue now closed! Thanks :D