There are many uses for email deliverability validation, but one great (and simple) example is how to set up email validation on WordPress registrations.
I have put together a super-simple, minimum abstraction email verification API wrapper for Proofy’s email validation API that you can use for free (although you’ll need an account and API key from Proofy – but you can get that free with some starter credits – and additional blocks of checks are pretty cheap).
Get started
So, you’ve got the API wrapper (linked above) and you have a Proofy account. Let’s get started.
First, you need to initialize the object class for the API wrapper. I’d recommend doing that hooked to the WordPress init
action and globalizing the object:
add_action( 'init', function() { // Your user ID from Proofy $aid = 12345; // Your API key from Proofy $api_key = 'some_random_api_key'; // Include the api wrapper object class. (Assumes you have this file in this directory) require 'path/to/proofy/class-rocketgeek-proofy-email-validation.php'; // Initiate the object class with your api key. global $proofy; $proofy = new RocketGeek_Proofy_Email_Verification_API( $aid, $api_key ); });
One thing to note with the above – make sure you have the right path to the API’s object class for the include. Otherwise, it won’t find it and things will break.
Now that the API object is initialized, you can use it anywhere (mostly). For purposes of this example, we’re going to check the validity of emails used to register through WP’s native registration process. For that, we hook into WP’s registration_errors
filter so we can add an error if the email turns out to be undeliverable (i.e. fake from some jerk spammer trying to sign up to your site and steal your free stuff).
/** * Validates email in native WP registration using Proofy.io API. * * @param array $errors A WP_Error object containing any errors encountered during registration. * @param string $sanitized_user_login User's username after it has been sanitized. * @param string $user_email User's email. * @return array $errors A WP_Error object containing any errors encountered during registration. */ add_filter( 'registration_errors', function( $errors, $sanitized_user_login, $user_email ) { // Assumes you have initialized the object class as global. global $proofy; $result = $proofy->verify( $user_email ); if ( ! is_array( $result ) ) { $errors->add( 'email_validation', __( 'Email validation could not be completed.', 'your-text-domain' ) ); } else { if ( 1 != $result['result'][0]['status'] ) { $errors->add( 'email_validation', __( 'Email is not deliverable or status is unknown.', 'your-text-domain' ) ); } } return $errors; },10, 3);
Hopefully, you can follow what’s going on in there. All we did was verify the deliverability through Proofy’s API with the wrapper’s $proofy->verify()
method. If it returned a status of “1”, then the email is legit. If it is anything else, we return an error. It could be 2, 3, or 4, which would be the following:
- 2: risky
- 3: undeliverable
- 4: unknown
So really, “3” is the only one we really have to return an error for, but keeping the example simple, we’re returning any of those as junk.
I hope you found this to be simple – and useful. The cleaner you keep your list, the better your email deliverability will be. And if your deliverability is kept in top shape, you shouldn’t have to worry about getting dropped or banned from your email service.
If you’d like me to write a custom application for you using this (or other) API, get started with 1-on-1 consulting at rocketgeek.com.
Leave a Reply