What are some common mistakes made when sending emails using the mail() function in PHP, and how can they be avoided for successful delivery?

One common mistake when using the mail() function in PHP is not setting the "From" header correctly. This can result in emails being marked as spam or not delivered at all. To avoid this issue, make sure to set the "From" header with a valid email address.

$to = "recipient@example.com";
$subject = "Subject";
$message = "Hello, this is a test email!";
$headers = "From: sender@example.com\r\n";

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