61 lines
1.9 KiB
PHP
Executable File
61 lines
1.9 KiB
PHP
Executable File
|
|
<?php
|
|
header('Content-Type: application/json');
|
|
|
|
class EmailValidator {
|
|
public function validate($email) {
|
|
$result = ['email' => $email, 'valid' => false, 'reason' => '', 'mx' => [], 'disposable' => false];
|
|
|
|
// Format check
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$result['reason'] = 'Invalid format';
|
|
return $result;
|
|
}
|
|
|
|
$domain = substr($email, strpos($email, '@') + 1);
|
|
|
|
// MX check
|
|
if (!getmxrr($domain, $mxhosts, $weights)) {
|
|
$result['reason'] = 'No MX records';
|
|
return $result;
|
|
}
|
|
$result['mx'] = $mxhosts;
|
|
|
|
// Disposable check
|
|
$disposable = ['tempmail.com', 'guerrillamail.com', 'mailinator.com', '10minutemail.com', 'throwaway.email', 'temp-mail.org', 'fakeinbox.com'];
|
|
if (in_array($domain, $disposable)) {
|
|
$result['disposable'] = true;
|
|
$result['reason'] = 'Disposable email';
|
|
return $result;
|
|
}
|
|
|
|
$result['valid'] = true;
|
|
$result['reason'] = 'Valid';
|
|
return $result;
|
|
}
|
|
|
|
public function bulkValidate($emails) {
|
|
$results = [];
|
|
foreach ($emails as $email) {
|
|
$results[] = $this->validate(trim($email));
|
|
}
|
|
return $results;
|
|
}
|
|
}
|
|
|
|
$action = $_POST['action'] ?? $_GET['action'] ?? '';
|
|
$validator = new EmailValidator();
|
|
|
|
switch ($action) {
|
|
case 'validate':
|
|
echo json_encode($validator->validate($_POST['email'] ?? $_GET['email']));
|
|
break;
|
|
case 'bulk':
|
|
$emails = isset($_POST['emails']) ? (is_array($_POST['emails']) ? $_POST['emails'] : explode("\n", $_POST['emails'])) : [];
|
|
echo json_encode(['results' => $validator->bulkValidate($emails)]);
|
|
break;
|
|
default:
|
|
echo json_encode(['actions' => ['validate', 'bulk'], 'example' => '?action=validate&email=test@example.com']);
|
|
}
|
|
|