1

I am trying to write a code for my mail body. i have a php file in called mailfunction.php.

In that file i have created a variable which has my html code as follow

 $message_body ="<html><body>"
."<table class='tg' style='border-style: dotted;'>"
          ."<tr><td class='tg-3zav'>Civilité</td><td class='tg-3zav'>" . strip_tags($titre) . "</td></tr>"
          ."<tr><td class='tg-3zav'>Prénom</td><td class='tg-3zav'>" . strip_tags($prenom) . "</td></tr>"
          ."<tr><td class='tg-3zav'>Nom</td><td class='tg-3zav'>" . strip_tags($nom) . "</td></tr>"
          . "</table></body></html>";

But unfortunatly my mail received mail didn't encode the htlm tags. I have received some text contained html tags which are not encoded as follow

<table class="tg" style="border-style: dotted;">
  <tr>
    <th class="tg-3zav">Civilité</th>
    <th class="tg-3zav">M</th>
  </tr>
  <tr>
    <td class="tg-3zav">Nom</td>
    <td class="tg-3zav">Frank</td>
  </tr>
  <tr>
    <td class="tg-3zav">Prénom</td>
    <td class="tg-3zav">Betrix</td>
  </tr>
</table>

Is there any idea to write this html code in the php file?

  • 1
    possible duplicate of [Php mail: how to send html?](http://stackoverflow.com/questions/4897215/php-mail-how-to-send-html) –  Feb 23 '15 at 10:14
  • 1
    You have to declare that its an HTML e-mail. See the link @caCtus posted above to find out how to do it. – Styphon Feb 23 '15 at 10:16

2 Answers2

4

You need to use the headers in mail function

<?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";

$message ="<html><body>"
."<table class='tg' style='border-style: dotted;'>"
          ."<tr><td class='tg-3zav'>Civilité</td><td class='tg-3zav'>" . strip_tags($titre) . "</td></tr>"
          ."<tr><td class='tg-3zav'>Prénom</td><td class='tg-3zav'>" . strip_tags($prenom) . "</td></tr>"
          ."<tr><td class='tg-3zav'>Nom</td><td class='tg-3zav'>" . strip_tags($nom) . "</td></tr>"
          . "</table></body></html>";

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

headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
I'm Geeker
  • 4,601
  • 5
  • 22
  • 41
  • Why set the headers like that? Why not just have `headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset=UTF-8\r\n";` Works perfectly fine. – Styphon Feb 23 '15 at 10:17
  • Thank you very much it works. Infact i have i have changed my content-type to 'text/html' – Devarajan Sekaran Feb 23 '15 at 10:43
  • Here is my corrected full code $headers .= 'From: ADMIN SERVER' . "\r\n"; $headers .= 'Cc: cc-cc@admin-server.org' . "\r\n"; $headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n"; $message_body ="" ."" ."" ."
    ";
    – Devarajan Sekaran Feb 23 '15 at 10:47
  • Cool you must add the header type on mail :) thanks for upvote :) – I'm Geeker Feb 23 '15 at 10:48
0

Thank you very much. Here I post my the corrected source code

$headers .= 'From: ADMIN SERVER' . "\r\n";
$headers .= 'Cc: cc-cc@admin-server.org' . "\r\n";
$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
$message_body ="<html><body>"
                ."<style type='text/css'>"
                ."tg {border-collapse:collapse;border-spacing:0;}"
                ."</style>"
                ."<table class='tg' style='border-style: dotted;'>"
                ."<tr><td class='tg-3zav'>Civilité :</td><td class='tg-3zav'>" . strip_tags($titre) . "</td></tr>"
                ."<tr><td class='tg-3zav'>Prénom : </td><td class='tg-3zav'>" . strip_tags($prenom) . "</td></tr>"
                ."<tr><td class='tg-3zav'>Nom : </td><td class='tg-3zav'>" . strip_tags($nom) . "</td></tr>"
                ."</table></body></html>";
David
  • 3,285
  • 1
  • 37
  • 54