0

I had set up email sending in localhost and using that I can access my gmail account to send mail using php script.

<?php

$to = "example@gmail.com";
$subject = "HTML Mail";
$message  = "<html><body>";
$message .= "<b>Hey! How are you today?</b>";
$message .= "<br>Regards";
$message .= "</body></html>";

$headers = "From: myemail@gmail.com\r\n";


mail($to,$subject,$message,$headers);
echo "<h1>MAIL SENT</h1>";
?>

When I send this email using this script, it gives out

<html><body><b>Hey! How are you today?</b><br>Regards</body></html>

Why html type output is not shown?

Hey! How are you today?
Regards

Like this in gmail?

Siren Brown
  • 403
  • 2
  • 5
  • 18

3 Answers3

2

Could it be that you are missing headers to specify this is HTML content?

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

Edit: As pointed by another answer, both headers are needed.

Acapulco
  • 3,373
  • 8
  • 38
  • 51
1

From the PHP documentation for mail():

// 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";

If these headers are missing, the message content is interpreted as plain text.

ul90
  • 762
  • 3
  • 8
1

Hi for sending email with HTML body you need to specify headers. Something like this

//your code <br>
$headers = "From: myemail@gmail.com\r\n"; <br>
//HTML headers <br>
$headers .= 'MIME-Version: 1.0' . "\r\n"; <br>
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

so after that you can send the email with html body

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

good luck

gengisdave
  • 1,969
  • 5
  • 21
  • 22