What are common mistakes to avoid when programming PHP email functionality?

One common mistake to avoid when programming PHP email functionality is not properly sanitizing user input, which can leave your application vulnerable to injection attacks. To prevent this, always use functions like filter_var() or htmlspecialchars() to sanitize user input before using it in email headers or bodies.

// Sanitize user input before using it in email headers
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$name = htmlspecialchars($_POST['name']);

// Use sanitized input in email headers
$headers = "From: $name <$email>";