0

I am trying to send an html email. Mail is working properly but not going as html email and it is just displaying ".$message.". The data is coming from the mysql database.

My script is as follows:

$to       = $email;
$subject  = $subject;
$message  = "$message";

$htmlContent = '
<html>
<body>
".$message."
<br /><br />Regards<br /><b>TM</b>
</body>
</html>';
$headers  = 'From: myemail@gmail.com' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

if(mail($to, $subject, $htmlContent, $headers))
echo "Mail Sent";
else
echo "Problem sending email";
Sid Menon
  • 23
  • 2
  • 9
  • 1
    Interpolation is not done in single-quoted strings, putting double-quotes inside the single-quoted string does not change that – Tom Regner Mar 28 '18 at 08:39
  • 1
    Are you sure you want to build all that stuff from scratch? There a tons of superb libraries like Swiftmailer that will take care of all the internals of mails – Nico Haase Mar 28 '18 at 08:47
  • i tried to with phpmailer. But i dont know what was the problem, always i got error stating smtp error. – Sid Menon Mar 28 '18 at 08:49
  • i was trying phpmailer from localhost – Sid Menon Mar 28 '18 at 08:51

3 Answers3

0

You have mixed single and double quotes. Use only one type when concatenating strings.

$htmlContent = "
<html>
<body>
".$message."
<br /><br />Regards<br /><b>RainbowCTM</b>
</body>
</html>";
Finwe
  • 6,372
  • 2
  • 29
  • 44
  • Its displaying now. But not displaying html mail. Its just showing with the html tags. – Sid Menon Mar 28 '18 at 08:43
  • In what program are you displaying the e-mail? In Gmail and Thunderbird, the HTML is displayed correctly. Also see https://stackoverflow.com/questions/11238953/send-html-in-email-via-php – Finwe Mar 28 '18 at 09:04
0

To complete previous answer, there is a huge difference between single and double quotes:

  • Single quote not analyze characters inside, so if you did: echo '$message', it just display $message.
  • Double quotes analyze characters inside, so if you did: echo "$message", it displays the content of $message variable.
Jouby
  • 2,196
  • 2
  • 21
  • 33
0

Finwe is totally right. Explanation: single quotes always take the string as it is without processing variables or metasigns like \t or \n.

just a hint in addition: try using only \n and not \r\n since there was a bug in some PHP-version that produced erros. even if this is not a problem at the moment, it might become one after an update (depending on your PHP-version).

meistermuh
  • 393
  • 3
  • 11