What are the security concerns related to sending a copy of the form submission to the user's specified email address in PHP?

Sending a copy of the form submission to the user's specified email address in PHP can pose security concerns such as email injection attacks or exposing sensitive information to unintended recipients. To mitigate these risks, sanitize and validate the user's email input to prevent malicious code injection and ensure that the email address is valid before sending any sensitive information.

// Sanitize and validate the user's email input
$user_email = filter_var($_POST['user_email'], FILTER_SANITIZE_EMAIL);

// Check if the email address is valid
if(filter_var($user_email, FILTER_VALIDATE_EMAIL)) {
    // Send a copy of the form submission to the user's specified email address
    $to = $user_email;
    $subject = 'Copy of your form submission';
    $message = 'Thank you for your submission. Here is a copy of the details you provided: ' . print_r($_POST, true);
    $headers = 'From: your_email@example.com' . "\r\n" .
        'Reply-To: your_email@example.com' . "\r\n";

    mail($to, $subject, $message, $headers);
} else {
    // Handle invalid email address
    echo 'Invalid email address';
}