Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Help with code that processes both plain text and HTML

Help
nestman
2013-03-22
2013-03-22
  • nestman
    nestman
    2013-03-22

    Here is a snip of code that is causing me problems. Earlier in the code it queries the database and retrieves two emails. One row has a format = Plain and the other row has a format = HTML, for testing purposes. The line of code giving me grief is $mail->MsgHTML($body); If I use "This is bold test" instead of $body then it works fine. I do have a very simple testEmail.html in the same directory as this PHP script, so that is not the problem. Can you see anything else that may be causing the SMTP error to show, "Mailer Error () Message body empty" when processing a HTML email?

    if ($row[format] == "Plain") {

    $mail->Body = $row["message"];

    } else {

    $body = file_get_contents('testEmail.html');
    $body = preg_replace('/[]/','',$body);

    $mail->MsgHTML($body);
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
    }

     
    Last edit: nestman 2013-03-22
  • Marcus Bointon
    Marcus Bointon
    2013-03-22

    What do you do this for?

    $body = preg_replace('/[\]/','',$body);
    

    that's quite likely an invalid regex - you start a character class with [, but escape the closing ] with a backslash, so it's quite likely you're getting back an empty string. This is debug 101 - you could at least try echoing $body.

    If you're setting HTML and body parts yourself and are not embedding images, don't use MsgHTML, just set Body and altBody directly; MsgHTML overwrites altBody anyway.

    And make sure you're NOT using the version of PHPMailer from sourceforge as it's old and buggy - I recommend using my fork on Github.