Categories
HTTP Tips & Tutorials PHP Tips & Tutorials

PHP: Send HTML Email (mail)

As we all know the simplest approach to create an email message and send it out is to use the php mail() function. A typical usage example would be:

mail('[email protected]', 'Subject Title', 'Message body goes here.');

However, as the mail() function sends emails in text/plain mime type by default, if you include HTML code in the message body, it would not be interpreted as HTML at all. Instead, all the tags and attributes are displayed as they are.

To work around this and send HTML email with PHP mail() function, you will have to modify the message mime type to text/html. In practice, use the mail() function with an additional argument to set arbitrary headers for the email message:

$to = '[email protected]';
$subject = 'Purchase Successful!';
$message = '
<html>
<head>
	<title>Purchase Successful!</title>
</head>
<body>
	<p>Here are the order details:</p>
	<p> ... </p>
	<p><a href="http://www.example.com">back to store</a></p>
</body>
</html>
';

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

mail($to, $subject, $message, $headers);

Now the email dispatched to the recipient will have all the HTML in it working as expected.

By Yang Yang

Hello, I'm Yang. I build online businesses that please people. Want to join in and post some useful articles on Kavoir.com? Shoot me a message.

13 replies on “PHP: Send HTML Email (mail)”

To send HTML mail in PHP, you need to use some additional headers. Below is the code to send HTML mail in PHP.

$to = “[email protected]“;
$subject= “Subject of email”;
$message= “This is HTML mail.”;
$fromName = “Name of the sender”;
$fromEmail = “[email protected]“;

// To send HTML mail, the Content-type header must be set
$headers = “MIME-Version: 1.0″ . “\r\n”;
$headers .= “Content-type: text/html; charset=iso-8859-1″ . “\r\n”;

// Additional headers
$headers .=”From: $fromName ”.”\r\n”;

//Mail function
mail($to, $subject, $message, $headers);

This code is not working for sending long html message.
Please give me some idea to send long html message or you can say long html tables.
Your help is very appreciated.

try using something like this:

mail($to, $subject, $message, $headers);

$to = ‘[email protected]’;

$subject = ‘Website Change Reqest’;

$headers = “From: ” . strip_tags($_POST[‘req-email’]) . “\r\n”;
$headers .= “Reply-To: “. strip_tags($_POST[‘req-email’]) . “\r\n”;
$headers .= “CC: [email protected]\r\n”;
$headers .= “MIME-Version: 1.0\r\n”;
$headers .= “Content-Type: text/html; charset=ISO-8859-1\r\n”;

$message = ”;
$message .= ‘Hello, World!’;
$message .= ‘This is a cool way to write long html email’;
$message .= ”;

hope can help, regards

Filippo

thank you so much ÅŸukunu verdim piç 🙂 translate from google translate turkish to english

I am new to PHP,

i had a mail form-page with $to, $from, $subject and $x1, $x2, $x3 ( as costum headers).

of which i fill-up the required fields i.e $to, $from, $subject and a optional field $x1 and deliberately keep $x2, $x3 fields empty, now when i submit the form the mail is sent….so far no problems.

Now when i check the mail headers of the received mail i get to see all the headers i.e. $to, $from, $subject, $x1, $x2, $x3. Of which $x2 and $x3 off-course are with empty headers.

======================
Mail Headers from received mail

To: [email protected]
Subject: test-3
From: [email protected]
Reply-To: [email protected]
X-1: x1_header
X-2:
X-3:
======================

PHP Code
=============

$to = “[email protected]”;
$from = $email;
$subject = “Time Sheet”;

$message = ”;
$message .= ”;
$message .= “”.$name.”” . strip_tags($_REQUEST[“getDesignation”]) . ” “. strip_tags($_REQUEST[“getEmail”]) .” “. strip_tags($_REQUEST[“getPhone”]) .””;
$message .= “9 am – 11 am 11 am – 1 pm 2 pm – 4 pm 4 pm – 6 pm Work Location: “;
$message .= “” . strip_tags($txtarea1) . “” . strip_tags($txtarea2) . “” . strip_tags($txtarea3).”” . strip_tags($txtarea4). “” . strip_tags($txtarea5) . “”;
$message .= “”;
$message .= “”;

$headers = “From:” . $from;
mail($to,$subject,$message,$headers);
echo “Mail Sent.”;

IN Gmail
=============
Mr Admin Istrator IT Manager [email protected] 9198477633429 am – 11 am 11 am – 1 pm 2 pm – 4 pm 4 pm – 6 pm Work Location: Teatttttttttttttttttttttttttaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbcccccccccccccccccccddddddddddddddd

Please give a solution for that……

Comments are closed.