How can the PHP mail function be optimized to correctly display line breaks in emails containing textarea content?
When sending emails with textarea content using the PHP mail function, line breaks may not display correctly due to differences in line break characters between operating systems. To ensure proper display of line breaks, you can use the PHP function `nl2br()` to convert newline characters to HTML `<br>` tags before sending the email.
<?php
// Retrieve textarea content from a form
$textarea_content = $_POST['textarea_content'];
// Convert newline characters to HTML <br> tags
$textarea_content = nl2br($textarea_content);
// Set up email parameters
$to = 'recipient@example.com';
$subject = 'Email with Textarea Content';
$message = $textarea_content;
// Additional headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Send email
mail($to, $subject, $message, $headers);
?>