Results 1 to 6 of 6

Thread: sending html table in php mail

  1. #1
    Join Date
    Feb 2009
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question sending html table in php mail

    Hello all,

    I want to send a html table in php mail .

    Code:
    <html>
    
    <p align='center'> <img src='http://www.mydomain.com/Images/Logo.jpg' width='210' height='45'> </p>
    
    <table cellspacing="4" cellpadding="4" border="1" align="center">
      
      <tr>
      <td align="center">Name</td>
      <td align="center"> John</td>
      <td align="center">Age</td>
      <td align="center">20</td>
      </tr>
      
      <tr>
      <td align="center">Empid</td>
      <td align="center">0014 </td>
      <td align="center">Depart</td>
      <td align="center">IT</td>
      </tr>
      
      <tr>
      <td align="center">Salary</td>
      <td align="center">20000</td>
      <td align="center">Country</td>
      <td align="center">US</td>
      </tr>
      
    </table>
      
    </html>
    on sending it simply in the message & then opening the recieved mail ,
    the code is displayed in place of structured table.

    Please help me to send html in php mail so that on opening
    a structured table can be displayed in the recieved mail .


    Thanks & regards.
    Last edited by MSK7; 01-06-2010 at 05:51 AM.

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    To send an HTML email, you will need to change the headers so it doesn't send the standard text.

    Like this:

    PHP Code:
    <?php
    $to 
    'user@example.com';
    $subject 'My Email';
    $msg "<html>
    <head>
    <title>Title of email</title>
    </head>

    <body>
    <p align='center'> <img src='http://www.mydomain.com/Images/Logo.jpg' width='210' height='45'> </p>

    <table cellspacing=\"4\" cellpadding=\"4\" border=\"1\" align=\"center\">
      
      <tr>
      <td align=\"center\">Name</td>
      <td align=\"center\"> John</td>
      <td align=\"center\">Age</td>
      <td align=\"center\">20</td>
      </tr>
      
      <tr>
      <td align=\"center\">Empid</td>
      <td align=\"center\">0014 </td>
      <td align=\"center\">Depart</td>
      <td align=\"center\">IT</td>
      </tr>
      
      <tr>
      <td align=\"center\">Salary</td>
      <td align=\"center\">20000</td>
      <td align=\"center\">Country</td>
      <td align=\"center\">US</td>
      </tr>
      
    </table>
    </body>
    </html>"
    ;

    // Make sure to escape quotes

    $headers  'MIME-Version: 1.0' "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";
    $headers .= 'From: My Site Name <me@mysite.com>' "\r\n";

    mail($to$subject$msg$headers);

    ?>
    Should do the trick.

  3. #3
    Join Date
    Feb 2009
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Arrow reply

    Hello Schmoopy,

    Thanks for your reply.

    on using the method according your suggestion ,

    then also i am getting the html text code in the recieved mail instead of getting table .

    I think it some change in headers like
    Content-Type: multipart/alternative;
    can be used .
    but i am not sure .

    Please suggest me how to correct this problem.

    Thanks .

  4. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    I'm not sure how you went about implementing it. But I just uploaded that code to my server and sent it to my email and it came out as a table.

    Perhaps post the code you have now so we can take a look at what might be causing the problem.

  5. #5
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi MKS7,
    I do not see this php code working. I see you are missing ?> before <head> element. $msg = "<html> is without closing ". At the end again missing php notations after <body> </html>";

    I want to use this code but does not work. If it is working for you can you suggest something that it may work for me, Thanks and best regards.

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    the closing php tag ( ?> ) is not "missing": we do not want to leave the php script at this point. If we did, then the markup would be output to the user, which we do not want.

    The closing double quote is not missing either; it is in the proper place after the closing </html> tag.

    This code should work as written. Schmoopy says it worked as expected when he tested it, and I don't see any errors either.

    This is a very old thread, and the original poster's problem is likely solved. If you are trying to develop a similar script, you might want to start your own thread and describe the problems you are having.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •