What are common pitfalls when using the PHP mail() function for sending emails?
Common pitfalls when using the PHP mail() function include emails being marked as spam due to incorrect headers, emails not being delivered due to misconfigured mail server settings, and potential security vulnerabilities if user input is not properly sanitized. To ensure emails are not marked as spam, make sure to set the "From" header correctly with a valid email address. To avoid delivery issues, double-check your server's mail settings and make sure they are configured properly. Lastly, always sanitize user input to prevent potential security risks.
$to = "recipient@example.com";
$subject = "Testing PHP Mail";
$message = "This is a test email sent using the PHP mail() function.";
$headers = "From: sender@example.com\r\n";
// Send email
if (mail($to, $subject, $message, $headers)) {
echo "Email sent successfully!";
} else {
echo "Email sending failed.";
}