If you enjoyed this site, please consider donating $3. Any amount is appreciated. Thanks!

How To Send HTML Mail With PHP

Xah Lee, 2009-01-14

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 = <<<HHHHHHHHHHHHHH
<!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>
HHHHHHHHHHHHHH;

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

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

?>

Related essays:

2009-01
© 2007 by Xah Lee.