« Moving out some of the web site bloat | Main | Smoking, or no? The law insists we spell out the obvious »

April 30, 2007

Emailing as HTML (Web Page) - PHP example

If you want to send an email as HTML, you can do so by adding a content-type header.

<?php
 
$content = <<< EMC
<h1>Hello World</h1>an HTML email
Shwoing how the content can be sent
as a web page.
EMC;
 
$extras = "From: Graham Ellis \n";
$extras .= "Reply-to: Graham Ellis \n";
mail("graham@sheepbingo.co.uk","Web page in HTML Demo (control)",
$content,$extras);
 
$extras .= "Content-type: text/html\n";
mail("graham@sheepbingo.co.uk","Web page in HTML Demo (working)",
$content,$extras);
 
?>

Without the extra line, the email views like so:

But with it, I got

A whole host of extra considerations here - you might light to look at multipart emails (the sort that use several types in the same transmission - giving users the option of viewing as plain text - example). You need to consider how your recipients spam filters differently handle HTML to plain text emails, and indeed whether what you're sending is really necessary or could be considered to be spam.

Edit / note added - It's been pointed out to me that I mis-spelled the word "showing" and then compounded the problem by putting up a screen capture with the word mis-spelt. I'm allowing myself to take the view that showing a typing error in both the source and the resut isn't ENTIRELY bad as it confirms that the screen capture really are the result of my sources!

Posted by gje at April 30, 2007 05:31 AM

Comments

Well House Consultants Ltd. Copyright 2010