How can an email sender be specified in PHP when using the mail() function?

When using the mail() function in PHP to send emails, the sender can be specified by adding additional headers to the email. The "From" header can be set to specify the sender's email address. It is important to note that some email servers may override this header for security reasons.

$to = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";
$headers = "From: sender@example.com\r\n";

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