What are the potential pitfalls of sending emails using PHP and how can they be avoided?

One potential pitfall of sending emails using PHP is that the emails may end up in the recipient's spam folder. This can be avoided by properly setting up the email headers, including a valid "From" address and ensuring that the email content is not marked as spam by including relevant and useful information.

// Set up email headers to avoid spam folder
$to = "recipient@example.com";
$subject = "Subject line";
$message = "Email content here";
$headers = "From: yourname@example.com\r\n";
$headers .= "Reply-To: yourname@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

// Send the email
mail($to, $subject, $message, $headers);