Validating Email Addresses In PHP (The Right Way)

When verifying an email address there are two basic things you want to do.

  1. validate the structure of the address
  2. confirm the domain can actually receive email (this is the important part)

Now validating the structure of the address can be done with a regex (view on ReFiddle)

preg_match('/^([a-zA-Z0-9_])+([a-zA-Z0-9\.+_-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/', 'bob@example.com');  

Then confirming the domain is simple enough in php. You can just check the DNS record for a MX record

checkdnsrr('google.com','MX');  

Then it all comes together below