Email validation is an important part of web development. While working on applications, we should always check if the given email is valid or not before performing any operations on it. This is because fake/dummy emails are of no use and so we must check if the entered email is a real email or not.
If you are running a website, you must be aware of fake emails provided by users. Let’s say you have a sign-up form and users are trying to register by entering a fake email. Here, fake emails do not actually exist. As a result, to complete the registration, the activation link would never get clicked. But, these fake user entries are already inserted into the database.
This results in unnecessary records getting stored in your database. It wastes your server bandwidth and your database size grows unnecessarily. This can be a major problem with popular websites where the traffic is quite high. You must want to avoid this situation. It requires you to check if the email address really exists before proceeding.
Having said that, let’s study how to check if the email is valid or not using mailboxlayer API.
Getting Started
The mailboxlayer is a powerful email validation service for measuring email deliverability and quality. They provide both free and paid plans. In the free plan, 250 API requests are allowed per month. That means one can validate 250 emails per month. You can check out their plans and pick a suitable one.
To get started, you need to first sign up on the mailboxlayer website. Once logged in, you will get your own dashboard where you can see the activity and quota available on your account. You also get your unique API Access Key which will be required for interacting with the mailboxlayer API.
Check If Email is Valid using mailboxlayer API
For checking the real email, we need to give a call to the mailboxlayer API endpoint. In the request, a user needs to send an email address and the API key. Upon sending a request, mailboxlayer returns a response which will be something like the screenshot below.
For this tutorial, we are going to interact with mailboxlayer API using cURL, Guzzle, and JavaScript.
Using cURL
PHP cURL is helpful to interact with external servers. You can easily call the REST API and handle the response using cURL. Let’s hit the mailboxlayer API.
<?php
// set API Access Key
$access_key = 'YOUR_API_KEY';
// set email address
$email_address = 'EMAIL_ADDRESS';
$ch = curl_init('https://apilayer.net/api/check?access_key='.$access_key.'&email='.$email_address.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Receive the data:
$json = curl_exec($ch);
curl_close($ch);
// Decode JSON response:
$validationResult = json_decode($json, true);
if ($validationResult['format_valid'] && $validationResult['smtp_check']) {
echo "Email is valid";
} else {
echo "Email is not valid";
}
In the response, I am checking if the format_valid
and smtp_check
has value ‘true’. If they are true, it means the email is valid and it’s a real email.
Using this approach, you can protect your system from invalid emails and reduce the bounce rate.
Using Guzzle
Guzzle is an alternative to cURL. If you are using Guzzle, you don’t need to have a cURL extension on your server. It gives you better formatting of code compared to cURL. Install the Guzzle using the command below.
composer require guzzlehttp/guzzle
After this, you can interact with mailboxlayer API as follows.
<?php
require_once "vendor/autoload.php";
use GuzzleHttp\Client;
// set API Access Key
$access_key = 'YOUR_API_KEY';
// set email address
$email_address = 'EMAIL_ADDRESS';
$client = new Client([
// Base URI is used with relative requests
'base_uri' => 'https://apilayer.net',
]);
$response = $client->request('GET', '/api/check', [
'query' => [
'access_key' => $access_key,
'email' => $email_address,
]
]);
$body = $response->getBody();
$validationResult = json_decode($body, true);
if ($validationResult['format_valid'] && $validationResult['smtp_check']) {
echo "Email is valid";
} else {
echo "Email is not valid";
}
Using JavaScript
If you don’t want to use PHP then you can do the same task through JavaScript. I am using the Fetch API to perform our API operation.
var access_key = 'YOUR_API_KEY';
var email_address = 'EMAIL_ADDRESS';
fetch('https://apilayer.net/api/check?access_key=' + access_key + '&email=' + email_address)
.then(response => response.json())
.then(data => {
if(data.format_valid && data.smtp_check) {
console.log('email is valid.');
} else {
console.log('email is not valid.');
}
});
I hope you understand how to check if an email is valid or not using mailboxlayer. Please share your thoughts and suggestions in the comment below.
Related Articles
- How to Get Real-Time Crypto Coin Market Rates in PHP
- Kickbox – A Real Time Email Verifier Library for PHP
- How to Get Geolocation from IP in PHP
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.
Please, I don’t know where or how can I declare a callback function on PHP. Please!
Thanks U