Send HTML email with PHP

This tutorial will guide you to how to write PHP script to send email with HTML format. Rather than ordinary old-fashion text format, HTML email are more attractive, this is good if you want to impress or attract your customer.

The PHP function to send email is mail() function. Below is the mail’s function description from the PHP manual:

mail() -- send mail

Description
bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])

mail() automatically mails the message specified in message to the receiver specified in to. Multiple recipients can be specified by putting a comma between each address in to. Email with attachments and special types of content can be sent using this function. This is accomplished via MIME-encoding.

mail() returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

Sending email is very simple as 1 2 3, here is the example :

<?php
mail("user@hostcom", "Hello Subject", "Hello, this is test from php script", "From:youruser@yourhost.com");
?>

The 4th parameter are needed if you not set default email from in your php.ini setting. Actually you can make funny things here, such as act were this email came from Bill Gates ? hehehe

<?php
mail("yourfriend@mailhost.com", "Hello There !", "Hello, You are not using the original copy of windows operating system !", "From:billgates@microsoft.com");
?>

Try it, you might chuckling at your self.

Sending a HTML email actually very simple, you just need a HTML code in your mail body, and the most important is you need to set content header and which character set will be use

Content-type: text/html; charset=iso-8859-1

More on this you can add the Carbon Copy (CC:), Blind Carbon Copy at the email headers

<?php

/**
 * @author sapta
 * @copyright 2009
 */

/* set recipients, from, CC:, BCC: */
$to  = "mary@example.com" . ", " ; // note the comma
$to .= "kelly@example.com";

$from = 'your.user@mailhost.com';
$cc = 'friend1@somehost.com, friend2@somehost.com';
$bcc = 'your.friend@blindhost.com';

/* set subject */
$subject = "Hi, test PHP mail";

/* message */
$message = '
<html>
<head>
<title>Test PHP eMail</title>
</head>
<body>
<h1>Hello !</h1>
<p>Hi guys, check this out, i can send a HTML message with PHP !!</p>
</body>
</html>
';

/* To send HTML mail, you need to set the Content-type header. */
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

/* additional headers */
$headers .= "To: ".$to."\r\n";
$headers .= "From: ".$from."\r\n";
$headers .= "Cc: ".$cc."\r\n";
$headers .= "Bcc: ".$bcc."\r\n";

/* finally, mail it! */
mail($to, $subject, $message, $headers);

?>

Ok, that’s it, try this out, let me know if you had a question.

Cascading Style Sheets (CSS) Definition

Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation (that is, the look and formatting) of a document written in a markup language. Its most common application is to style web pages written in HTML and XHTML, but the language can be applied to any kind of XML document, including SVG and XUL.

CSS is designed primarily to enable the separation of document content (written in HTML or a similar markup language) from document presentation, including elements such as the colors, fonts, and layout. This separation can improve content accessibility, provide more flexibility and control in the specification of presentation characteristics, enable multiple pages to share formatting, and reduce complexity and repetition in the structural content (such as by allowing for tableless web design). CSS can also allow the same markup page to be presented in different styles for different rendering methods, such as on-screen, in print, by voice (when read out by a speech-based browser or screen reader) and on Braille-based, tactile devices. While the author of a document typically links that document to a CSS stylesheet, readers can use a different stylesheet, perhaps one on their own computer, to override the one the author has specified.

CSS specifies a priority scheme to determine which style rules apply if more than one rule matches against a particular element. In this so-called cascade, priorities or weights are calculated and assigned to rules, so that the results are predictable.

The CSS specifications are maintained by the World Wide Web Consortium (W3C). Internet media type (MIME type) text/css is registered for use with CSS by RFC 2318 (March 1998).
Read the rest of this entry »