Send HTML Formatted Mail using PHP

HTML markup and styling can make an email more attractive. We can send simple text mail by using PHP's mail function. But what about HTML formatted mail! Can we send HTML formatted mail same way? The answer is YES! But we need to add some header information (including content type) for this. Here an simple example for sending HTML mail using PHP.

The htmlMail function:
 * HTML Mail function
 * @return bool | void
function htmlMail($to, $from_mail, $from_name, $subject, $message)
	$header = array();
	$header[] = "MIME-Version: 1.0";
	$header[] = "From: {$from_name}<{$from_mail}>";
	/* Set message content type HTML*/
	$header[] = "Content-type:text/html; charset=iso-8859-1";
	$header[] = "Content-Transfer-Encoding: 7bit";

	if( mail($to, $subject, $message, implode("\r\n", $header)) ){
		return true;

/* HTML Formatted Message */
$msg = "<html>\n<head>\n<title>Testing</title>\n</head>\n<body>\n";
$msg .= "<h1 style='color:red;'> Hello Horld! </h1>\n</body>\n</html>";

/* Send mail */
if ( htmlMail('', '', 'My Name', 'Mail Subject', $msg) ){
	echo "Mail Send!";
	echo "Mail Could not send!"


Go to top