What are the potential issues when using PHP scripts to send emails from a form on a website?

One potential issue when using PHP scripts to send emails from a form on a website is the risk of email injection attacks, where malicious users can inject additional headers into the email to manipulate its content or send spam. To prevent this, you should sanitize and validate user input before using it to construct the email headers.

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

// Check if the email is valid
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Construct email headers
    $to = 'recipient@example.com';
    $subject = 'Contact Form Submission';
    $headers = "From: $email\r\n";
    
    // Send email
    mail($to, $subject, 'Message body', $headers);
} else {
    // Handle invalid email address
    echo 'Invalid email address';
}