What could be causing emails sent through the mail() function to not be received by 1und1 email accounts?

Emails sent through the mail() function may not be received by 1und1 email accounts due to potential spam filtering or blocking by the email provider. To solve this issue, ensure that the email content is not flagged as spam, use proper email headers, and consider using a dedicated email service provider for better deliverability.

$to = 'recipient@example.com';
$subject = 'Test Email';
$message = 'This is a test email';
$headers = 'From: sender@example.com' . "\r\n" .
    'Reply-To: sender@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

if (mail($to, $subject, $message, $headers)) {
    echo 'Email sent successfully';
} else {
    echo 'Email delivery failed';
}