1

We have an email system where an email is formatted via PHPMailer. When PHPMailer sends the email, I open it in the email client and it displays as text not HTML.

I have tried using Gmail and Yahoo - both clients display the email as the HTML source code.

Does anybody have any suggestions as to how I can fix this?

Note: If I use Outlook to attach a version of the HTML email (insert as text), the email is sent and displayed correctly in the email clients listed above.

Thanks in advance

hinch
  • 91
  • 2
  • 12
  • change encoding from `text/plain` to `text/html` somewhere in your configuration. – shyammakwana.me Nov 09 '17 at 13:51
  • @shyammakwana.me This is already set in my code – hinch Nov 09 '17 at 13:58
  • Add your code to the question. We can't tell what you're doing otherwise. – Synchro Nov 09 '17 at 14:06
  • @Synchro This would be difficult to add the code because it is made up across multiple files and function, with variables that you would not know the value to. – hinch Nov 09 '17 at 14:09
  • 2
    That's your problem - don't make it ours. Run an independent test of a simple PHPMailer script (the example in the readme will do) using your values. If that works, you know that the problem is in your code, and it's then up to you to track it down; if it doesn't work - you have a nice example to post in your question. – Synchro Nov 09 '17 at 14:19

4 Answers4

2

Phpmailer, by default, sets the body of the email as plaintext. You need to specify the body as html by setting $eMail->IsHTML(true);

red9350
  • 338
  • 3
  • 11
0

I believe that PHPMailer is not recognizing you code as HTML. Try to add this line of code when creating the email:

$mail->IsHTML(true);  

EDIT Here is a sample of the code I am using and works correctly. You can cross check for any differences.

$mail = new PHPMailer;
//Set email parameters  
$mail->isSMTP();                            // Set mailer to use SMTP
$mail->Host = 'smtp.office365.com';      // Specify main and backup server
$mail->SMTPAuth = true;                          // Enable SMTP authentication
$mail->Username = 'example@mydomain.co.uk';     // SMTP username
$mail->Password = 'MyPassword';                  // SMTP password                      
$mail->SMTPSecure = 'tls';           // Enable encryption, 'ssl' also accepted
$mail->Port = 587;
$mail->From = $fromEmail;
$mail->FromName = self::getEmailName($email);
$mail->addAddress($email);
$mail->WordWrap = 50;                            // Set word wrap to 50 characters  
$mail->isHTML(true);                             // Set email format to HTML
$mail->CharSet = "UTF-8";
drkostas
  • 517
  • 1
  • 7
  • 29
  • I have this in my code already. Sorry i should have added this in my question – hinch Nov 09 '17 at 13:55
  • Check also if you are doing it after setting the body of the email. To set the body you should be using this line: `$mail->Body = $Body;` – drkostas Nov 09 '17 at 14:02
  • I currently have it like this @Kostas Georgiou.$mail->isHTML(true); // Set email format to HTML $mail->Subject = $subject; $mail->Body = $message; – hinch Nov 09 '17 at 14:04
  • Try to change the order and make it like this: `$mail->Subject = $subject; $mail->Body = $message; $mail->isHTML(true); // Set email format to HTML` – drkostas Nov 09 '17 at 14:08
  • @Kotas Georgiou Thanks for the suggestion but this did not work – hinch Nov 09 '17 at 14:14
  • Try to add these headers to your email: `$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";` Lastly, you can check a sample of a working code above on my answer. – drkostas Nov 09 '17 at 14:30
0

I know that's too late, but i was running into the same problem for an hour and thats what worked for me:

Be sure to declare $mail->IsHTML = true and $mail->AltBody after $mail->Body = $body;

like this:

    $mail->IsHTML = true;
    $mail->Body = $body;
    $mail->AltBody = $body;
0

This did it for me:

I had this:

$mail->SMTPDebug = 2;

So I removed it. And added this before the Body like:

$mail->isHTML(true);
$mail->Body = $message;

I was working with some PHPMailer code that Godaddy support gave me and they had commented out this:

  public function IsHTML($ishtml = true) {

$this->ContentType = 'text/plain';

   if ($ishtml) {

  $this->ContentType = 'text/html';

} else {

  $this->ContentType = 'text/plain';

}



}

The smtp via Godaddy is still very very delayed, like sometimes 20 minutes.

Good Luck‼️

Mattman85208
  • 1,858
  • 2
  • 29
  • 51