What are the best practices for incorporating query results into email messages in PHP?
When incorporating query results into email messages in PHP, it is important to properly format the data to ensure readability and clarity for the recipient. One best practice is to loop through the query results and construct the email message dynamically using HTML or plain text formatting. Additionally, consider sanitizing the data to prevent any potential security vulnerabilities.
// Assume $queryResults is an array of query results
$message = "Here are the query results:<br><br>";
foreach($queryResults as $result) {
$message .= "Name: " . $result['name'] . "<br>";
$message .= "Email: " . $result['email'] . "<br><br>";
}
// Construct the email
$to = "recipient@example.com";
$subject = "Query Results";
$headers = "From: sender@example.com\r\n";
$headers .= "Content-type: text/html\r\n";
// Send the email
mail($to, $subject, $message, $headers);