What are the common pitfalls to avoid when implementing email notifications in PHP form submissions?

One common pitfall to avoid when implementing email notifications in PHP form submissions is not properly sanitizing user input, which can lead to security vulnerabilities such as SQL injection or cross-site scripting attacks. To solve this issue, always use proper input validation and sanitization functions before sending any user input in an email notification.

// Sanitize user input before using it in the email notification
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);

// Send email notification
$to = "recipient@example.com";
$subject = "New form submission";
$body = "Name: $name\nEmail: $email\nMessage: $message";
$headers = "From: sender@example.com";

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