How to check if an email address exists without sending an email ?
In this article, we will learn to check if an address exists without sending an email.
Approach: By using some free APIs we can easily check whether an email is valid or not. One of the easiest way to check whether an email address is valid is to use PHP’s filter_var() function.
Example 1:The following method can only check if the format is a valid email format. In this, we cannot check whether the format actually exits or not.
PHP
<?php
$email1 = "gfgrohit@gmail.com" ;
$email2 = "gfg.com" ;
function checkIfValid( $email ){
if (filter_var( $email , FILTER_VALIDATE_EMAIL) ){
echo "Email is Valid\n" ;
}
else {
echo "Email is not valid\n" ;
}
}
checkIfValid( $email1 );
checkIfValid( $email2 );
?>
|
Output
Email is Valid
Email is not valid
Example 2: To check if an email exists, we need to use some APIs into our PHP program. We are going to use a free API called abstractapi. The curl_init() function returns a cURL handle. The curl_setopt() set an option for a cURL transfer. The curl_setopt_array() sets multiple options for a cURL transfer. This function is useful for setting a large number of cURL options without repetitively calling curl_setopt().
PHP
<?php
$email = 'sarkarrohit650@gmail.com' ;
if (!filter_var( $email , FILTER_VALIDATE_EMAIL)){
exit ( "Invalid" );
}
echo "Valid Email format\n" ;
$ch = curl_init();
$apiKey = "5f92aa70f05f4b8eb45e6d6f09ec8a08" ;
curl_setopt_array( $ch , [
CURLOPT_URL => "https:
api_key= $apiKey &email= $email ",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true
]);
$res = curl_exec( $ch );
curl_close( $ch );
$data = json_decode( $res , true);
if ( $data [ 'deliverability' ] === 'UNDELIVERABLE' ){
exit ( "EmailID $email does not exist" );
}
exit ( "EmailID $email exists" );
|
Output
Valid Email format
EmailID sarkarrohit650@gmail.com exists
If we execute the command
echo $res ;
It contains all the information about our email
Output
{"email":"sarkarrohit650@gmail.com","autocorrect":"","deliverability":"DELIVERABLE","quality_score":"0.70","is_valid_format":{"value":true,"text":"TRUE"},"is_free_email":{"value":true,"text":"TRUE"},"is_disposable_email":{"value":false,"text":"FALSE"},"is_role_email":{"value":false,"text":"FALSE"},"is_catchall_email":{"value":false,"text":"FALSE"},"is_mx_found":{"value":true,"text":"TRUE"},"is_smtp_valid":{"value":true,"text":"TRUE"}}
Example 3: We can also check the value of $data[‘is_smtp_valid’][‘value’] to check whether the email exists or not
PHP
<?php
$email = 'sarkarrohit650@gmail.com' ;
if (!filter_var( $email , FILTER_VALIDATE_EMAIL)){
exit ( "Invalid" );
}
echo "Valid Email format\n" ;
$ch = curl_init();
$apiKey = "5f92aa70f05f4b8eb45e6d6f09ec8a08" ;
curl_setopt_array( $ch , [
CURLOPT_URL => "https:
api_key= $apiKey &email= $email ",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true
]);
$res = curl_exec( $ch );
curl_close( $ch );
$data = json_decode( $res , true);
if ( $data [ 'is_smtp_valid' ][ 'value' ]){
exit ( "EmailID $email does not exist" );
}
exit ( "EmailID $email exists" );
|
Output
Valid Email format
EmailID sarkarrohit650@gmail.com exists
Last Updated :
13 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...