How can carriage return characters affect the functionality of PHP email headers?

Carriage return characters in PHP email headers can cause issues such as headers being split into multiple lines, which can lead to the email not being formatted correctly or even being rejected by email servers. To solve this problem, you can use the PHP function `trim()` to remove any unwanted whitespace characters, including carriage returns, from the headers before sending the email.

// Remove carriage return characters from email headers
$headers = "From: sender@example.com" . "\r\n";
$headers .= "Reply-To: replyto@example.com" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8" . "\r\n";

// Remove any unwanted whitespace characters
$headers = trim($headers);

// Send the email
$success = mail("recipient@example.com", "Subject", "Message", $headers);

if($success) {
    echo "Email sent successfully";
} else {
    echo "Failed to send email";
}