How can the validity of email addresses be checked before sending emails in PHP?

Before sending emails in PHP, you can check the validity of email addresses by using the filter_var() function with the FILTER_VALIDATE_EMAIL filter. This function will return true if the email address is valid and false if it is not. By validating email addresses before sending emails, you can ensure that your emails reach the intended recipients without any issues.

$email = "example@example.com";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Email address is valid, proceed with sending the email
    echo "Email address is valid.";
} else {
    // Email address is invalid, handle the error accordingly
    echo "Email address is invalid.";
}