What are common pitfalls in PHP code that may lead to duplicate email sending?

One common pitfall in PHP code that may lead to duplicate email sending is not properly checking if an email has already been sent before sending it again. To prevent this, you can implement a check to see if the email has already been sent before sending it again.

// Check if the email has already been sent before sending it again
if (!$emailSent) {
    // Send email
    $to = 'recipient@example.com';
    $subject = 'Subject';
    $message = 'Message';
    $headers = 'From: sender@example.com';

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

    // Set a flag to indicate that the email has been sent
    $emailSent = true;
}