tags:

views:

259

answers:

2

Hi folks, I've used this example on the PHP site to send an HTML email, but when I recieve a mail sent by the script it renders the tags in the client (Outlook)

<?php
// multiple recipients
$to  = '[email protected]' . ', '; // note the comma
$to .= '[email protected]';

// 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 <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>

My client is set up to recieve HTML mail - so I'm not sure what's going on. Any pointers?

+3  A: 

I don't know how to fix your existing script but for sending HTML E-Mails, I always recommend using a ready-made class like PHPMailer: It provides safe and clean handling of multi-part mails, multiple recipients, and even file attachments.

Pekka
I second this answer. Mailing is so complex that using an existing class is always a good idea. Personally I've used Zend_Mail lately, but it's up to you to find what you like best.
DaDaDom
+1  A: 

Your code works for me. If Outlook has a feature to see the original source code of a message, you should inspect it carefully and see if there's something wrong.

Sometimes the e-mail client would ignore some headers if you happen to mix Windows and Unix style line feeds.

Also:

  • Don't add the To header explicitly. PHP will do it for you.

  • Remove the leading carriage returns in $headers.

Álvaro G. Vicario