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:

<?php

$fromAddr = 'admin@example.com'; // the address to show in From field.
$recipientAddr = 'xah@xahlee.org';
$subjectStr = 'Thank You';

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

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

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

?>

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