0

I have just started using PHPMailer and I cant get my HTML emails to send and render properly. I can get them to send fine but when I add the isHTML(true) method the email doesnt send at all. Is there anything I need to put inside the HTML email or layout differently.

Here is the html email:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>This is a test email</h1>
    <p>Congratulations, {NAME}</p>
    <p>You have won {PRIZE}</p>
    <img src="{DOMAIN}assets/images/generated_barcodes/actual-000000013.png" alt="Barcode">
</body>
</html>

Here is the sequence of methods I use to send the email:

$this->_mail = new PHPMailer();
$this->_mail->From = FROM_EMAIL;
$this->_mail->FromName = FROM_EMAIL_NAME;
$this->_mail->addAddress($email);
$this->_mail->Subject = $subject;
$this->_mail->Body = $body;
$this->_mail->isHTML(true);

$this->_mail->send()

Just to clarify this email sends fine if I comment out the isHTML(true) method but it obviously doesn't render the HTML.

Thank you in advance!

EDIT

Seems like I just had to use the $this->_mail->msgHTML($body, DOMAIN); method to load in my HTML. But now I have a new issue. The email sends but not while in an AJAX request. I need it to send an email when I post some data with AJAX.

David Jones
  • 4,115
  • 6
  • 24
  • 50
  • 2
    Don't you need to capitalize `Is`? – TylerH Jul 01 '14 at 13:25
  • What do you mean by "doesn't send"? Is there any error? Also try setting up `$this->_mail->AltBody` (if I remember correct) – ex3v Jul 01 '14 at 13:25
  • Maybe you got to set isHtml before adding Body content i was wrong http://stackoverflow.com/questions/11140263/phpmailer-sending-html-code , as TylerH said it might not work because you got small letter i instead of capital I – Chris Jul 01 '14 at 13:28
  • @TylerH not according to the example on github, but ill give it a try! – David Jones Jul 01 '14 at 13:28
  • @DavidJones I only ask because I've only ever seen it as `IsHTML`... could be wrong :-) – TylerH Jul 01 '14 at 13:28
  • @ex3v nope no error, I have tried wrapping it in a try catch and the $this->_mail->ErrorInfo is NULL – David Jones Jul 01 '14 at 13:29
  • 1
    @TylerH yep your right, weird how the github documentation writes it as isHTML and not IsHTML! Thanks for the help! – David Jones Jul 01 '14 at 13:30
  • Try to check if there are any errors in errors.log – ex3v Jul 01 '14 at 13:31
  • I'll post it as an answer so it can be accepted, then. PHP is weird – TylerH Jul 01 '14 at 13:31
  • It may have been `IsHTML` in older versions, but it is definitely `isHTML` now, see [here](https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php#L650). It's also not capitalised anywhere in documentation. Are you using an old version? – Synchro Jul 01 '14 at 15:32
  • Nope, I just downloaded a fresh version, see my comments on the answer form TylerH – David Jones Jul 01 '14 at 15:35

1 Answers1

3

You should capitalize Is for $this->_mail->IsHTML(true);.

TylerH
  • 19,065
  • 49
  • 65
  • 86
  • Not quite sure why this would make any difference - PHP is case-insensitive for function/method names. – Synchro Jul 01 '14 at 15:25
  • Yeh sorry I dont think this was the issue, seems I was a bit premature accepting an answer. Seems like this did not solve my issue. Although one email did send but they seemed to have stopped again. – David Jones Jul 01 '14 at 15:30
  • Using this seems to have solved it $this->_mail->msgHTML($body, DOMAIN); – David Jones Jul 01 '14 at 15:32