Failed to send HTML mails using PHP mail()

Strange things happening to me. I’m trying to send a HTML mail, using the php mail() function, but no luck here. Even when I copy/paste a piece of code literally, it doesn’t work. What am I doing wrong? Here is the piece of code I use:

        $message = "<html><body>";
        $message .= "<table rules='all' style='border-color: #666;' cellpadding='10'>";
        $message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td></tr>";
        $message .= "<tr><td><strong>Email:</strong> </td></tr>";
        $message .= "</table>";
        $message .= "</body></html>";       

        $to = '[email protected]';

        $subject = 'Website Change Reqest';

        $headers = "From: " . $email . "\r\n";
        $headers .= "Reply-To: ". $email . "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

        if (mail($to, $subject, $message, $headers)) {
          echo 'Your message has been sent.';
        } else {
          echo 'There was a problem sending the email.';

And this is what my e-mail looks like…:

Reply-To: [email protected]

MIME-Version: 1.0

Content-Type: text/html; charset=ISO-8859-1
Message-Id: <[email protected]>
Date: Mon, 4 Jul 2011 01:45:51 +0200 (CEST)

<html><body><table rules="all" style="border-color: #666;" cellpadding="10"><tr style='background: #eee;'><td><strong>Name:</strong> </td></tr><tr><td><strong>Email:</strong> </td></tr><tr><td><strong>Type of Change:</strong> </td></tr><tr><td><strong>Urgency:</strong> </td></tr><tr><td><strong>URL To Change (main):</strong> </td></tr><tr><td><strong>NEW Content:</strong> </td></tr></table></body></html>

What do I do wrong?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

We can’t see where the variable $email was set, but I’m guessing that there might be an extra line break at the end of your $email variable. This would have the effect of putting in two linebreaks after the From: header and before the Reply-to: , which signals the start of the body of the message and the completion of the headers.


$email = trim($email);

before constructing the message. Since there appears to be an extra line break after the Reply-to header as well, my case is even stronger for an extra break in $email.


Also try changing the linebreaks to PHP’s native format on the system where the code will run. This is done by replacing \r\n with PHP_EOL

    $headers = "From: " . $email . PHP_EOL;
    $headers .= "Reply-To: ". $email . PHP_EOL;
    $headers .= "MIME-Version: 1.0" . PHP_EOL;
    $headers .= "Content-Type: text/html; charset=ISO-8859-1" . PHP_EOL;

Solution 2

Here is a useful link to sending html emails in php

Solution 3

I recommend you to use a mailer class. They give you the possibility to use smtp auth, so every mail will be passed through. A few examples:

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply