How to send html table in email body in php?
Stefan Bogdanescu
Founder & Senior Architect
How to Send Dynamic HTML Tables in Email Body Using PHP: A Developer's Guide
Sending dynamic content, especially complex structures like HTML tables, via email can often lead to frustrating issues where the recipient sees the raw code instead of a nicely formatted table. As a senior developer, I’ve encountered this problem repeatedly. It usually stems from how the MIME headers and the content itself are structured during the mail delivery process.
In this post, we will dissect why your HTML table might be appearing as plain text and provide robust solutions to ensure your dynamic tables render correctly in the recipient's inbox. We will cover the pitfalls of raw output and demonstrate best practices for reliable email generation in PHP.
The Root Cause: Why You See Raw Code
The issue you are facing—receiving the HTML code instead of a rendered table—is almost always related to how the email client interprets the Content-Type header, or sometimes, the presence or absence of proper surrounding HTML structure.
When you use PHP's mail() function, the server sends the content as raw data. If the headers are not perfectly configured, or if the output string is not a self-contained, valid HTML document (i.e., it lacks the necessary <html>, <head>, and <body> wrappers), the email client defaults to displaying the literal text rather than interpreting it as markup.
Your provided code snippet attempts to construct the entire HTML document from scratch:
$output = '<html\><body\><form>';
// ... table construction ...
$output .= '</table></form></body></html>';
print $output;
While this approach is valid, ensuring every line break and tag is perfectly escaped and contained within the email payload is critical for cross-client compatibility.
Solution 1: Ensuring Proper HTML Framing
The most reliable way to send HTML tables is to ensure that the entire content sent via mail() is a complete, well-formed HTML document. Your current approach of building the structure manually is correct, but we must ensure the output is clean and correctly encoded for email transmission.
When sending emails, always set the Content-Type header explicitly to text/html. This tells the receiving mail client how to interpret the body content.
Here is a refined approach focusing on robust HTML generation:
<?php
// Start with the full HTML structure
$output = "<html><head><style>table,th,td{border:1px solid black;border-collapse:collapse}</style></head><body>";
// Start the form or table content
$output .= "<form>";
$output .= "<table>";
$output .= "<tr><th>Author</th><th>Node Title</th><th>Node Summary</th><th>Node Body</th><th>Edit this node</th><th>Report Abuse</th><th>Group</th></tr>";
while($row = mysql_fetch_array($sql)) {
// Ensure all dynamic content is safely embedded within <td> tags
$output .= "<tr><td><a href=\"http://localhost/localstage/user/" . $row['uid'] . "\">" . $query['name'] . "</a></td><td><a href=\"http://localhost/localstage/node/" . $row['nid'] . "\">" . $row['title'] . "</a> (New)</td><td></td>";
// ... continue building the rest of the row data ...
$output .= "</tr>";
}
$output .= "</table></form></body></html>";
$to = 'hmdnawaz@gmail.com';
$headers = "From Ahmad \r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
// Send the complete, framed HTML output
mail($to, 'Email report', $output, $headers);
?>
Notice how we wrapped everything in <html>, <head>, and <body>. This structure provides the necessary context for email clients to render the markup correctly as a visual table.
Solution 2: Modernizing with Frameworks (The Laravel Approach)
While manually constructing HTML works, managing complex data structures and ensuring security in large applications is much safer when using modern frameworks. For instance, in a framework like Laravel, you would never write this raw string concatenation directly into your mail function. Instead, you would use Blade templates to render the table structure, passing only the data to the mail service. This separation of concerns makes code cleaner, less error-prone, and significantly more secure against XSS attacks.
When building complex views or components, frameworks encourage separating presentation logic from business logic. Think about how robust data handling systems like those found in Laravel manage large datasets; they ensure that the output is always contextually correct, which applies equally to email generation. This principle of clean separation is core to why modern PHP applications thrive.
Conclusion
Sending HTML tables via PHP requires meticulous attention to detail regarding the surrounding HTML structure and HTTP headers. By ensuring your output is a complete document (<html><body>...</body></html>) and setting the Content-Type header correctly, you guarantee that email clients will render your dynamic table as intended. For professional development, especially when dealing with complex data and communication, adopting structured approaches—like those emphasized by powerful ecosystems such as Laravel—will save you countless headaches down the line.