What role does the use of \r\n and \n play in constructing email headers in PHP, and how can they impact the appearance of the email?

Using \r\n or \n in constructing email headers in PHP is important for specifying line breaks. The use of \r\n is the standard line break for email headers, while \n may not be recognized by all email servers. Incorrect line breaks can result in headers being displayed incorrectly or the email being marked as spam. To ensure proper formatting of email headers, always use \r\n when constructing them in PHP.

// Construct email headers with \r\n for line breaks
$headers = "From: sender@example.com\r\n";
$headers .= "Reply-To: reply-to@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

// Send the email
mail($to, $subject, $message, $headers);