Email address is an essential part of any web from. We are almost always required to enter an email address for anything that we sign up for; contact form, account creation form, subscription form, etc. In a lot of cases it does make sense to require a valid email address. It is often required for communication and for security purpose, other times it is sole purpose is to market a certain product(s) (what you can call spam).
For this reason, a lot of users instinctually enter a bogus email address just to finish the process. While I don’t blame users for doing so, I also provide this class that allows developers to validate an email address and make sure it actually can receive emails.
I myself use fake email address in a lot of situations. I use things like <something>@example.com and <anything>@mailinator.com, and others. I have my own version of mailinator service 🙂
But, if you rightfully need a valid email address from the user, you can verify that the email address provided is valid. Here is how:
Is a PHP class that can be easily used to verify an email address and make sure it is valid and does exist on the mail server.
This class connects to the mail server and checks whether the mailbox exists or not.
How to use:
Initialize the class:
$ve = new VE\VerifyEmail('firstname.lastname@example.org', 'email@example.com');
OR (you can specify other port number than 25)
$ve = new VE\VerifyEmail('firstname.lastname@example.org', 'email@example.com', 26);
The first email address ‘firstname.lastname@example.org‘ is the one to be checked, and the second ‘email@example.com‘ is an email address to be provided to the server. This email needs to be valid and from the same server that the script is running from. To make sure your server is not treated as a spam or gets blacklisted check the score of your server here https://mail-tester.com
Then you call the verify function:
This will restun a boolean. True if the email is valid, false otherwise.
If you want to get any errors, call this function after the verify function:
This will return an array of all errors (if any):
Array (  => No suitable MX records found. )
If you want to get all debug messages of the connection, call this function:
This will return an array of all messages and values that used during the process.
Array (  => initialized with Email: firstname.lastname@example.org, Verifier Email: email@example.com, Port: 25  => Verify function was called.  => Finding MX record...  => Found MX: alt4.gmail-smtp-in.l.google.com  => Connecting to the server...  => Connection to server was successful.  => Starting veriffication...  => Got a 220 response. Sending HELO...  => Response: 250 mx.google.com at your service  => Sending MAIL FROM...  => Response: 250 2.1.0 OK gw8si3985770wjb.84 - gsmtp  => Sending RCPT TO...  => Response: 250 2.1.5 OK gw8si3985770wjb.84 - gsmtp  => Sending QUIT...  => Looking for 250 response...  => Found! Email is valid. )
You can also modify/set the values of email to verify, verifier email, and port number on the fly after initializing
$ve->set_email('<another email Address to verify>'); $ve->set_verifier_email('<another verifier email>'); $ve->set_port(<port number>)
- Some mail servers will silently reject the test message, to prevent spammers from checking against their users’ emails and filter the valid emails, so this function might not work properly with all mail servers.
- You server must be configured properly as a mail server to avoid being blocked or blacklisted. This includes things like SSL, SPF records, Domain Keys, DMARC records, etc. To check your server use this tool https://mail-tester.com