What are common pitfalls when trying to send a formatted table as an email in PHP?
Common pitfalls when trying to send a formatted table as an email in PHP include not properly setting the content type of the email to HTML, not properly formatting the table markup, and not handling special characters or escaping data properly. To solve this issue, make sure to set the content type of the email to HTML using the `Content-Type: text/html` header, properly format the table markup using HTML tags, and ensure that any data being inserted into the table is properly escaped to prevent any HTML injection vulnerabilities.
<?php
// Set content type to HTML
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Define your table markup
$table = "<table>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
<tr>
<td>John Doe</td>
<td>john.doe@example.com</td>
</tr>
</table>";
// Send email with table
$to = "recipient@example.com";
$subject = "Table Example";
$message = "<html><body><h1>Table Example</h1>".$table."</body></html>";
mail($to, $subject, $message, $headers);
?>