How to send html table in email body in php?

How to send html table in email body in php?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Rendering HTML Tables in Email Clients: A Practical Guide for PHP Developers

You are encountering a classic problem in email development: the recipient sees raw HTML tags instead of rendered content. In your specific case, the root cause is a conflict between how email clients parse headers and how standard web browsers render pages. Even though your code explicitly sets Content-Type to text/html, many email clients—particularly Outlook on Windows—default to rendering emails as text if they cannot immediately determine that HTML is supported or if the MIME structure is slightly off.

The most robust solution involves changing the content type from text/html to multipart/alternative. This format allows you to send both a plain-text version and an HTML version of your email. Email clients will automatically detect which one they can handle best. If a client prefers text, it strips the HTML; if it supports rich formatting, it renders the table.

Here is how you should structure the headers for your mail() function to ensure compatibility:

$headers = "From: Ahmad <ahmad@example.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable\r\n";

The Multipart Alternative Approach

While setting Content-Type to text/html is necessary, it is not sufficient on its own. The multipart/alternative header tells the mail transfer agent (MTA) that the message contains multiple parts representing the same content in different formats. This is the industry standard for ensuring your HTML tables render correctly while maintaining fallbacks for restrictive clients.

You would need to construct a more complex string for the third argument of the mail() function, concatenating the plain text and HTML versions separated by a boundary string. However, if you are working within a modern PHP application stack, using a library like SwiftMailer or Laravel's Mail facade is significantly safer than writing raw strings. These libraries handle the multipart encoding automatically, ensuring that special characters in your table cells (like < or >) are properly escaped to prevent clients from interpreting them as HTML tags themselves.

Security and Data Handling

Your current code snippet includes several critical security vulnerabilities that must be addressed before sending any email, regardless of the rendering issue. You are using the deprecated mysql_* functions, which were removed in PHP 7.0 and are insecure by default. Furthermore, you are concatenating user input directly into SQL queries without sanitization:

$query = mysql_query("SELECT title from node Where type = 'groupnode' AND nid = '" . $row['nid'] . "'");

This approach invites SQL injection attacks. In a production environment, you must use prepared statements with PDO or MySQLi. Additionally, outputting raw variables like $query['title'] into HTML can lead to Cross-Site Scripting (XSS) if the data comes from an untrusted source. Always escape output using htmlspecialchars() before embedding it in your HTML string.

Encoding and Special Characters

Your current code sets the charset to ISO-8859-1. This is a legacy encoding that supports only Western European languages and lacks characters like accented letters or symbols common in many modern applications. You should switch to UTF-8, which is universally supported by modern email clients. Ensure your database connection also uses UTF-8, and set the charset explicitly in your HTML meta tags if you were generating a full page, though for inline emails, the header is usually enough.

Modern Alternatives

If you are maintaining legacy code using mail() functions, fixing the headers and switching to multipart/alternative will likely resolve the rendering issue immediately. However, if you have the option to refactor, consider moving your email logic into a framework like Laravel or Symfony. The laravelcompany.com platform often serves as a reference for modern PHP development patterns where these complexities are abstracted away. Frameworks provide dedicated mail channels that handle view rendering, template engines (like Blade), and the necessary MIME multipart construction without requiring you to manually string together HTML tags and headers.

By shifting from raw mail() calls to structured frameworks or properly configured libraries, you eliminate the guesswork regarding header formats and encoding, ensuring your dynamic HTML tables appear exactly as intended in every inbox.

Our network