Back to the top
References:
Some email clients can render HTML documents as web pages, including images and Javascript and applets and so forth. Now, never mind the security risks in allowing programs sent in emails to run when they're received, and never mind the fact that so much spam is sent as HTML that many people are beginning to automatically filter it out unread, how does one actually go about sending an HTML email?
The manual page already given offers an example of a simple HTML email (Example 4). Note in particular the lines that follow the comment "To send HTML mail, the Content-type header must be set".
That's pretty much it for a straight HTML email. For images and external stylesheets it is often suggested using a URL that links to a file on your site. Spammers use this method in order to find out if an email was received and read (even if you don't respond). You might want to embed the images instead.
There's another problem. People who do not use HTML-capable email clients, or who deliberately operate their email without HTML rendering turned on, will obviously not get a properly-renderered page. If you were playing nice you'd have included a plain-text version of the email for them to read, and put it first so that it would be at the top when they read it and so be able to ignore all the other stuff that followed.
The problem with embedding images is that email is a text-only format. You have to do something like base64-encode anything that isn't plain text so that it will fit, and base64-encoding anything makes it 25% bigger, because it turns every three bytes into four. This is true for attachments as well. Just something to keep in mind.
Now, I'm not going to go into the full details of how to put together a multi-part, embedded-image HTML email, because there is already an article on this site about exactly that (I've given the reference above). Needless to say, there are also classes and suchlike scripts about that already implement all the MIME stuff, and boundary-marking and whatnot that is involved. This is a good thing, because to do it properly can be a real boring job. One, a version of a class written by Richard Heyes, is included as part of the PEAR, so if you've got that installed you're pretty much home and towelling yourself off.
You'll also find (at the two sites linked to in the previous paragraph) classes designed to assist with sending large numbers of emails, since - as noted on its manual page - mail() on its own isn't really good for high-volume traffic. But that's another story.