Why are certain lines, such as Message-Id and X-UIDL, added to emails when using the mail() function in PHP?

The Message-Id and X-UIDL lines are added to emails sent using the mail() function in PHP to uniquely identify each email message. The Message-Id helps in tracking and referencing emails in email clients and servers, while X-UIDL is used for identifying and preventing duplicate emails from being processed. These lines are automatically generated by the mail server to ensure proper email handling and tracking.

// Example PHP code snippet to send an email with Message-Id and X-UIDL headers
$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";
$headers = "Message-ID: <" . uniqid() . "@example.com>\r\n";
$headers .= "X-UIDL: " . md5(uniqid(rand(), true)) . "\r\n";
$headers .= "From: sender@example.com\r\n";

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