PHP-Bibliothek — Composer-Paket
composer require nsoftware/email-validator-php
Typsichere API-Client für email-validaton.com. Asynchron, mit Caching, Laravel-tauglich.
Installation
composer require nsoftware/email-validator-php
Voraussetzungen: PHP 8.1+, Guzzle 7, PSR-6-Cache (optional), PSR-3-Logger (optional).
Schnellstart
<?php
require 'vendor/autoload.php';
use NSoftware\EmailValidator\Client;
$client = new Client('ev_your_api_key');
// Verify one email
$result = $client->verify('user@example.com');
if ($result->isValid()) {
echo "Valid — confidence: {$result->confidence}";
}
if ($result->isDisposable()) {
echo "Disposable email — reject!";
}
Asynchrone Verifizierung
$client->verifyAsync('user@domain.com')
->then(fn($result) => echo $result->status);
// Bulk async with controlled concurrency
$client->verifyBulkAsync($emails, concurrency: 10)
->then(function(array $results) {
foreach ($results as $email => $r) {
echo "$email: {$r->status}\n";
}
});
PSR-6-Caching
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
$cache = new FilesystemAdapter('email-validator', 300);
$client = new Client('ev_...', cache: $cache);
// Account info auto-cached for 5 min
$account = $client->getAccount();
// Force refresh
$account = $client->getAccount(forceRefresh: true);
Typsichere DTOs
| Klasse | Eigenschaften |
|---|---|
VerificationResult |
email, status, reason, confidence, mxServer, mxList, smtpResponse, sourceIp, durationMs, remainingCredits, isYahoo |
AccountInfo |
email, plan, planName, creditsRemaining, creditsUsed, creditsLimit, planType, subscriptionActive, subscriptionCancelled, subscriptionExpires, freeRemaining, totalRemaining, isAdmin |
CreditInfo |
hasCredits, creditsRemaining |
Jedes DTO besitzt eine statische fromApiResponse(array) factory and toArray() serialization.
Praktische Prädikate
| Methode | Rückgabewert | Erklärung |
|---|---|---|
isValid() | bool | status === 'valid' |
isInvalid() | bool | status === 'invalid' |
isRisky() | bool | status === 'risky' |
isCatchAll() | bool | status === 'catch_all' |
isDisposable() | bool | status === 'disposable' |
isDeliverable() | bool | valid || risky || catch_all |
costCredit() | bool | valid || invalid || over_quota || disposable |
Laravel-Integration
Das Paket registriert sich über die Laravel-Auto-Discovery selbst. Fügen Sie einfach Ihren API-Schlüssel zu .env:
EMAIL_VALIDATOR_API_KEY=ev_your_key
Verwenden Sie nun überall Dependency Injection oder die Facade:
use NSoftware\EmailValidator\Client;
use EmailValidator; // Facade alias
class SignupController
{
public function store(Request $request, Client $validator)
{
$result = $validator->verify($request->email);
if (!$result->isDeliverable()) {
return back()->withErrors(['email' => 'Invalid email']);
}
// Create user...
}
}
// Or with the facade:
$result = EmailValidator::verify('user@example.com');
$account = EmailValidator::getAccount();
Fehlerbehandlung
| Ausnahme | HTTP | Wann |
|---|---|---|
AuthenticationException | 401/403 | Ungültiger oder fehlender API-Schlüssel |
RateLimitException | 429 | 60 Anfragen/Min. oder stündliches Limit. $e->retryAfterSeconds |
NoCreditsException | 429 | Guthaben aufgebraucht |
ApiException | -- | Netzwerkfehler, unerwartete Antwort |
try {
$result = $client->verify('user@example.com');
} catch (RateLimitException $e) {
sleep($e->retryAfterSeconds);
// retry
} catch (NoCreditsException $e) {
// prompt user to upgrade
} catch (ApiException $e) {
logger()->error("Validation failed: {$e->getMessage()}");
}
Paketinhalt
| Ordner | Beschreibung |
|---|---|
src/Client.php | Haupt-API-Client mit synchronen und asynchronen Methoden |
src/Data/ | Unveränderliche DTOs (VerificationResult, AccountInfo, CreditInfo) |
src/Exceptions/ | Typisierte Ausnahmen mit HTTP-Codes |
src/Laravel/ | Service Provider, Facade, veröffentlichbare Konfiguration |
tests/ | PHPUnit-Tests |
config/ | Laravel-Konfiguration (Veröffentlichung mit Artisan) |
v5.0.0 · MIT-Lizenz · PHP 8.1+ · Guzzle 7 · PSR-6 / PSR-3