Log in

View Full Version : sending html table in php mail



MSK7
01-06-2010, 05:41 AM
Hello all,

I want to send a html table in php mail .



<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.

Schmoopy
01-06-2010, 09:33 AM
To send an HTML email, you will need to change the headers so it doesn't send the standard text.

Like this:



<?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.

MSK7
01-06-2010, 10:37 AM
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 .

Schmoopy
01-06-2010, 04:42 PM
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.

Amjad
04-30-2011, 01:24 PM
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.

traq
04-30-2011, 08:17 PM
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.