How can developers ensure reliable email delivery when using PHP to send emails via a hosting provider like 1und1, considering the limitations of the mail() function?

When using the mail() function in PHP to send emails via a hosting provider like 1und1, developers can ensure reliable email delivery by setting up proper email headers, including a valid "From" address, and potentially using a third-party email service provider like SendGrid or Amazon SES for more robust email delivery capabilities.

// Set up email headers
$headers = 'From: yourname@example.com' . "\r\n" .
    'Reply-To: yourname@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

// Send email using mail() function
$to = 'recipient@example.com';
$subject = 'Test Email';
$message = 'This is a test email.';
$mailSent = mail($to, $subject, $message, $headers);

if($mailSent) {
    echo 'Email sent successfully.';
} else {
    echo 'Email delivery failed. Please check your email settings.';
}