We help IT Professionals succeed at work.

What am I doing wrong with this email script?

Bruce Gust
Bruce Gust used Ask the Experts™
Here's my code:

$to = $email;
$subject = "Your Showdown registration!";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <bruce@countryshowdown.com>' . "\r\n";
$subject = "MyShowdown password";
$message .= file_get_contents('email_header.php');
$message .= "<P>Your username is: <b>your email address</b><BR>Your ";
$message .= "password is: <b>$contestant_password</b>!<P>If you have any questions, you can contact your Showdown staff by clicking <A HREF=\"http://www.countryshowdown.com/Contact.php\" target=\"_blank\">here</a>.<P>Thanks!";
$message .= file_get_contents('email_footer.php');
mail($to, $subject , $message, $headers);

While I get the email, it shows up without a "from" value and the content is all text, none of the html code is rendered correctly.

What am I doing wrong?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I Don't think that you should have the < and > around the email address for the From, and I am not sure if this will make a difference or not but sometimes things like this are case sensitive so it should be Content-Type not Content-type in the $headers

Dave BaldwinFixer of Problems
Most Valuable Expert 2014

It is not formatted properly as an HTML page.  Just putting HTML elements in there isn't enough.  You also have $subject in there twice.

This is what your message body should look like when it is done.  If you wanted to get more complex, there is a lot of to learn about HTML email.  It is not just like making web pages or just putting some HTML codes in there.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<P>Your username is: <b>your email address</b><BR>
Your password is: <b>contestant_password</b>!
<P>If you have any questions, you can contact your Showdown staff by clicking <A HREF="http://www.countryshowdown.com/Contact.php" target="_blank">here</a>.

Open in new window

   $headers = 'MIME-Version: 1.0'. PHP_EOL;
   $headers .= 'Content-type: text/html; charset=iso-8859-1'. PHP_EOL;
   $headers .= 'FROM: bruce@countryshowdown.com'. PHP_EOL;

Open in new window

No less than greater than as jrm said, also I use PHP_EOL instead of "\r\n" because I've had problems with \r\n in the past.

Also, of interest may be that you define $subject twice, i don't know which one is right but Thought you would want to know.


I can show you 5+ working examples on my site where HTML emails are working without declaring DOCTYPE and having a head section or a defined body section (primarily because ONLY body will be included in HTML email parsing anyway)
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

@Derokorian, I'm trying to give him something that is most likely to work.  I don't care about other examples, I can find plenty of them myself.  I also didn't cover multi-part MIME format which is the recommended way to send HTML email because it would take more explaining than it would be worth in this question.
Bruce GustPHP Developer


The PHP_EOL made all the difference!