Sending HTML mail

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

lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

why doesn't anybody google any mroe?

Code: Select all

/* To send HTML mail, you can set the Content-type header. */
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mail() wrote:

Code: Select all

<?php
/* recipients */
$to  = "mary@example.com" . ", " ; // note the comma
$to .= "kelly@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, you can set the Content-type header. */
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";                                // <-------- HERE

/* 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";

/* and now mail it */
mail($to, $subject, $message, $headers);
?>
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

lostboy - I would Google, but that's a pretty general search ('content-type headers')...
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Thanks lostboy and Feyd.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

pickle wrote:I don't know about you, but I'm referring to server-based spam filters, such as Spam-Assasin, or Gee-Whiz. I know for a fact that an email consisting entirely of HTML gets an email flagged high as possible SPAM, although it doesn't guarantee. Client side spam filters might act differently though.
So am I. I am no expert on Spam-filters, but flagging an email as spam simply because its HTML would pretty much defeat the purpose of spam-filtering to my mind.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

patrik,

I have to mention that as soon as I placed the HTML headers on my e-mails, my Hotmail automatically placed it in my junkmail inbox. If I don't place the HTML headers on there (still with HTML code), it still displays HTML but places it in my normal inbox. Strange though I agree.

Cheers

Mark
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Okay,

I've implemented the headers, and Hotmail puts it into the junkmail folder.

However, for some reason the same problem is still occurring in Outlook Express, so I'm a little confused here. Could there be any other reason why Outlook isn't picking up the HTML?

Many thanks

Mark
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

Make sure that you include all the headers. Hotmail is picky if it doesn't receive all the headers in the email

Code: Select all

$headers .= "MIME-Version: 1.0\n"; 
  $headers .= "Content-type: text/plain; charset=iso-8859-1\n"; 
  $headers .= "X-Priority: 3\n"; 
  $headers .= "X-MSMail-Priority: Normal\n"; 
  $headers .= "X-Mailer: php\n"; 
  $headers .= "From: "".$myname."" <".$myemail.">\n";
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

patrikG wrote: So am I. I am no expert on Spam-filters, but flagging an email as spam simply because its HTML would pretty much defeat the purpose of spam-filtering to my mind.
I should clarify that an email consisting only of HTML will not get automatically flagged as SPAM because of that fact. Say, for example, an email needs a score of 4 to become SPAM. Spam filters will generally (although this is modifiable) give an email a score of 3 based on the HTML.

So, while it is possible to send an email only of HTML, you have to make sure your message is personal enough so that it won't get flagged.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

lostboy - thanks, your modified headers solved the Hotmail junk folders problem. However, the e-mail is still not being detected as HTML from Outlook Express. I've been told by a computer researcher friend of mine that Outlook seems to be a bit strange in this respect. Is there any way of solving this however? I have seen HTML e-mails work fine in Outlook, but mine doesn't seem to.

Cheers

Mark
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

Code: Select all

Mime-Version: 1.0
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit    //<---- I just checked the properties on one i just got
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Hi

I've tried implementing the header changes by putting in the Content-Transfer-Encoding information, however it's still not showing up as HTML in Outlook!

Bloomin' MS...

Mark
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

care to post the current version of your code?
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Code: Select all

/* To send HTML mail, you can set the Content-type header. */
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-MSMail-Priority: Normal\n";
$headers .= "X-Mailer: php\n";
$headers .= "Content-Transfer-Encoding: 7bit\n";

/* additional headers */
$headers .= 'To: '.$name.' <'.$toadr.">\r\n";
$headers .= 'From: '.$row['Firstname'].' '.$row['Lastname'].' <'.$fromadr.">\r\n";
$headers .= "Cc: ".$cc."\r\n";
$headers .= "Bcc: ".$bcc."\r\n";

ini_set('sendmail_from', $fromadr );
mail($toadr,$subj,$message,$headers);
$message contains HTML code for the e-mail.

Cheers

Mark
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Not entirely sure here, but try:

Code: Select all

/* To send HTML mail, you can set the Content-type header. */ 
$headers  = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
$headers .= "X-Priority: 3\r\n"; 
$headers .= "X-MSMail-Priority: Normal\r\n"; 
$headers .= "X-Mailer: php\r\n"; 
$headers .= "Content-Transfer-Encoding: 7bit\r\n"; 

/* additional headers */ 
$headers .= 'To: '.$name.' <'.$toadr.">\r\n"; 
$headers .= 'From: '.$row['Firstname'].' '.$row['Lastname'].' <'.$fromadr.">\r\n"; 
$headers .= "Cc: ".$cc."\r\n"; 
$headers .= "Bcc: ".$bcc."\r\n"; 

ini_set('sendmail_from', $fromadr ); 
mail($toadr,$subj,$message,$headers);
Post Reply