HTML email not working with php's mail function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zachalbert
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2009 11:32 pm

HTML email not working with php's mail function

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: HTML email not working with php's mail function

Post by Chris Corbyn »

Try using \r\n instead of just \n.

Also, Content-Type, not Content-type ;)
zachalbert
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2009 11:32 pm

Re: HTML email not working with php's mail function

Post 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";
 
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

Re: HTML email not working with php's mail function

Post 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);
?>
 
zachalbert
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2009 11:32 pm

Re: HTML email not working with php's mail function

Post by zachalbert »

Okay, using that example worked! Now I just need to customize it for my use...

Thanks for the help.
Post Reply