Page 1 of 1
HTML email not working with php's mail function
Posted: Wed Jan 14, 2009 12:13 am
by zachalbert
Hi there. I'm new to php, but i've been beating my head against the wall trying to get this email to send as html. Here's the code so you can tell me what I'm doing wrong.
Code: Select all
<?php
$ToName = "Me";
$ToEmail = "myemail@address.com";
$ToSubject = "Mail Test";
$EmailBody = "<h1>Yo.</h1>";
$FromName = "Me";
$FromEmail = "me@mydomain.com";
$header = "To: ".$ToName." <".$ToEmail.">\n"."From: ".$FromName." <".$FromEmail.">\n"."MIME Version: 1.0\n"."Content-type: text/html; charset=iso-8859-1\n";
mail($ToEmail,$ToSubject,$EmailBody,$header);
?>
What shows up is the last 2 lines of the header & the body of the email, like so:
***
MIME Version: 1.0
Content-type: text/html; charset=iso-8859-1
<h1>Yo.</h1>
***
Why doesn't isn't it being read as html?
Re: HTML email not working with php's mail function
Posted: Wed Jan 14, 2009 12:19 am
by Chris Corbyn
Try using \r\n instead of just \n.
Also, Content-Type, not Content-type

Re: HTML email not working with php's mail function
Posted: Wed Jan 14, 2009 12:54 am
by zachalbert
Hm... I tried that but still no luck. Here's my header with the revisions so you can see:
Code: Select all
$header = "To: ".$ToName." <".$ToEmail.">\r\n"."From: ".$FromName." <".$FromEmail.">\r\n"."MIME Version: 1.0\r\n"."Content-Type: text/html; charset=iso-8859-1\r\n";
Re: HTML email not working with php's mail function
Posted: Wed Jan 14, 2009 1:11 am
by paqman
Here's an example from php.net for sending html mail using the mail() function:
Code: Select all
<?php
// multiple recipients
$to = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
Re: HTML email not working with php's mail function
Posted: Wed Jan 14, 2009 12:33 pm
by zachalbert
Okay, using that example worked! Now I just need to customize it for my use...
Thanks for the help.