-1

I am setting up a confirmation email script for our customers to receive an email when they complete the online form. I uploaded my script and tested but I received an email with just the raw HTML. I have been looking at some tutorials trying to do this as well. I validated my HTML and I passed all the tests. Is there something that I have to include in my PHP for my body string to be interpreted as HTML?

Here is my script

<?php
//Script for sending a confirmation email
$body = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
 <head>
  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />
  <title>Demystifying Email Design</title>
  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>
</head>
<body style=\"margin: 0; padding: 0; background-color: #ecf0f1;\">
 <table cellpadding=\"0\" cellspacing=\"0\" width=\"60%\" align=\"center\" style=\"border: 1px solid #bdc3c7;\">
  <tr bgcolor=\"#3498db\">
   <td align=\"center\">
    <img alt=\"Logo\" src=\"http://www.****.com/logo-lg.jpg\" style=\"width: 50%; height: auto; padding-top: 5%; padding-bottom: 5%;\" />
   </td>
  </tr>
  <tr bgcolor=\"#ffffff\" align=\"center\">
   <td>
    <h3 style=\"padding-top: 5%; padding-bottom: 5%;\">Worldwide innovator in flexible liquid packaging</h3>
   </td>
  </tr>
  <tr align=\"center\" bgcolor=\"#ffffff\">
   <td>
    <h4 style=\"padding-top: 5%; padding-bottom: 5%;\">Thank you for contacting customer service. We've received your sample request; one of our team members will be in contact with you in the very near future. Thanks again for your time and interest.</h4>
   </td>
  </tr>
 </table>
</body>
</html>";

$to = "*******@****.com";
$subject = "Thanks for Reaching out";

if (mail($to,$subject,$body)){
    echo "Mail was sent";
} else{
    echo "Failed";
}

This is the email I received

This is the email I got

John Saunders
  • 160,644
  • 26
  • 247
  • 397
George
  • 115
  • 2
  • 9
  • Did you specify the content type in the headers? check https://css-tricks.com/sending-nice-html-email-with-php/ – Adon May 22 '15 at 17:42
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advanced". – John Saunders May 22 '15 at 18:39
  • My apologizes for poor grammar and saying thank you. – George May 26 '15 at 12:53

2 Answers2

2

You need to also set a header indicating this is HTML, e.g.

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";
mail($to, $subject, $message, $headers);

Source (see Example 3).

Eric J.
  • 147,927
  • 63
  • 340
  • 553
1

Correct code will be:

$to = "*******@****.com";
$subject = "Thanks for Reaching out";

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

if (mail($to,$subject,$body,$headers)){
   echo "Mail was sent";
}
Samir Das
  • 1,878
  • 12
  • 20