PHP: Send HTML Mail

By Xah Lee. Date:

This page shows you how to send HTML mail with PHP.

Sending html email in php is extremely easy. All you have to do is call the “mail” function with some extra header. Here's a simple working example:


$fromAddr = ''; // the address to show in From field.
$recipientAddr = '';
$subjectStr = 'Thank You';

$mailBodyText = <<<HHHHHHHHHHHHHH
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Thank You</title>
<b>Your login is:</b> {$_POST['login']}<br>
<b>Password:</b> {$_POST['password']}<br>

$headers= <<<TTTTTTTTTTTT
From: $fromAddr
MIME-Version: 1.0
Content-Type: text/html;

mail( $recipientAddr , $subjectStr , $mailBodyText, $headers);


Note: the <<<HHHHHHHHHHHHHH there is a string quoting mechanism. It's called heredoc. See: PHP: String Syntax, Heredoc.