How can PHP headers be properly set to avoid emails being rejected by email providers like GMX?

When sending emails via PHP, headers should be set properly to ensure emails are not rejected by email providers like GMX. To avoid rejection, include a valid "From" address, set the "Reply-To" header to a valid email address, and ensure the "Return-Path" header is set to a valid email address. Additionally, consider using a library like PHPMailer to handle email sending, as it provides better support for setting headers correctly.

<?php
$to = 'recipient@example.com';
$subject = 'Subject of the Email';
$message = 'This is the content of the email.';

$headers = "From: sender@example.com\r\n";
$headers .= "Reply-To: replyto@example.com\r\n";
$headers .= "Return-Path: returnpath@example.com\r\n";

mail($to, $subject, $message, $headers);
?>