Using PHP to Validate and Block Disposable Emails
This document provides example code for how to use PHP to validate an email and determine if it is from a disposable email provider using the Check-Mail API.
The following are two functions: one to verify an email address and another to determine if the email should be blocked based on the API response.
<?php
// Function to get the email information
function verify_email($email) {
$apiKey = "YOUR_API_KEY";
$url = "https://api.check-mail.org/v1/?email=" . urlencode($email);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer " . $apiKey
]);
// Execute the request
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
return null;
}
curl_close($ch);
// Decode and return the response
return json_decode($response, true);
}
// Function to just determine if an email should be blocked
function should_email_be_blocked($email) {
$emailData = verify_email($email)
if ($emailData && isset($emailData['block'])) {
return $emailData['block'];
}
return false;
}
// Example usage
$email = "example@temp-mail.org";
$responseData = verify_email($email);
if ($responseData) {
echo "Email: " . $responseData['email'] . "\n";
echo "Valid: " . ($responseData['valid'] ? 'Yes' : 'No') . "\n";
echo "Block: " . ($responseData['valid'] ? 'Yes' : 'No') . "\n";
echo "Disposable: " . ($responseData['disposable'] ? 'Yes' : 'No') . "\n";
echo "Risk Score: " . $responseData['risk'] . "\n";
} else {
echo "Invalid response from the API.";
}
Replace YOUR_API_KEY
with your actual API key and example@temp-mail.org
with the email address you want to validate.
Explanation
verify_email
function: Sends aGET
request to the Check-Mail API and returns the response as an array.should_email_be_blocked
function: Returns whether the email should be blocked based on theblock
field.- The response is decoded from JSON and displayed, showing if the email is valid, whether it should be blocked, if it's disposable, and the risk score.
Next Steps
- Integrate these PHP functions into your application to automate the detection of disposable email addresses.
- Refer to the documentation for further details on the API.