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 // valid format $email1 = "gfgrohit@gmail.com" ; // Invalid format $email2 = "gfg.com" ; // Function to check the format is valid or not 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 ); ?> |
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' ; //checks whether the email format is valid or not if (!filter_var( $email , FILTER_VALIDATE_EMAIL)){ exit ( "Invalid" ); } echo "Valid Email format\n" ; // Initialize cURL. $ch = curl_init(); // Unique Api Key is received from the server // once you access the service $apiKey = "5f92aa70f05f4b8eb45e6d6f09ec8a08" ; curl_setopt_array( $ch , [ // Set the URL that you want to GET // by using the CURLOPT_URL option. CURLOPT_URL => "https: //emailvalidation.abstractapi.com/v1? api_key= $apiKey &email= $email ", // Set CURLOPT_RETURNTRANSFER // so that the content is returned as a variable. CURLOPT_RETURNTRANSFER => true, // Set CURLOPT_FOLLOWLOCATION to true to follow redirects. CURLOPT_FOLLOWLOCATION => true ]); // Execute the request. $res = curl_exec( $ch ); // Close the cURL handle. curl_close( $ch ); $data = json_decode( $res , true); if ( $data [ 'deliverability' ] === 'UNDELIVERABLE' ){ exit ( "EmailID $email does not exist" ); } exit ( "EmailID $email exists" ); |
Valid Email format EmailID sarkarrohit650@gmail.com exists
If we execute the command
echo $res ;
It contains all the information about our email
{"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' ; //checks whether the email format is valid or not if (!filter_var( $email , FILTER_VALIDATE_EMAIL)){ exit ( "Invalid" ); } echo "Valid Email format\n" ; // Initialize cURL. $ch = curl_init(); // Unique Api Key received // from the server once you access the service $apiKey = "5f92aa70f05f4b8eb45e6d6f09ec8a08" ; curl_setopt_array( $ch , [ // Set the URL that you want to GET // by using the CURLOPT_URL option. CURLOPT_URL => "https: //emailvalidation.abstractapi.com/v1? api_key= $apiKey &email= $email ", // Set CURLOPT_RETURNTRANSFER so that // the content is returned as a variable. CURLOPT_RETURNTRANSFER => true, // Set CURLOPT_FOLLOWLOCATION to true to follow redirects. CURLOPT_FOLLOWLOCATION => true ]); // Execute the request. $res = curl_exec( $ch ); // Close the cURL handle. curl_close( $ch ); $data = json_decode( $res , true); if ( $data [ 'is_smtp_valid' ][ 'value' ]){ exit ( "EmailID $email does not exist" ); } exit ( "EmailID $email exists" ); |
Valid Email format EmailID sarkarrohit650@gmail.com exists
Please Login to comment...