Send HTML Email with PHP and mail()

January 25th, 2010 | Posted in PHP
2

The PHP mail() function is great for sending plain text emails but it will not send HTML emails without special headers.

Here is a PHP mail() with HTML example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$body_html = '
	<html>
		<head>
			<title>HTML Test Email</title>
			<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		</head>
		<body>
			<p>
				This is a test.
			</p>
		</body>
	</html>
';
 
$from = 'sender@example.com';
$to = 'recipient@example.com';
$subject = 'HTML Test Email';
$headers = "From:$from\n" .
    "MIME-Version: 1.0\n" .
    "Content-type: text/html; charset=iso-8859-1";
 
mail($to, $subject, $body_html, $headers);

Demo

Demo Send HTML Email with PHP and mail()

Other Interesting Posts

Comments (2)

  1. Jeremy says:

    Is this part necessary? mail($to, $subject, $body_html, $headers);

  2. Without the mail function the email will not be sent.

Leave a Reply