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 = <<<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);

?>

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

If you have a question, put $5 at patreon and message me.

  1. PHP Install
  2. PHP Basics
  3. String Syntax
  4. String Operations
  5. Array
  6. Hash Table
  7. Looping Thru Array
  8. Function
  9. Write Package
  10. Send HTML Mail
  11. Send Mail with Attachment
  12. MySQL Database Query