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

KlasseEigenschaften
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

MethodeRückgabewertErklärung
isValid()boolstatus === 'valid'
isInvalid()boolstatus === 'invalid'
isRisky()boolstatus === 'risky'
isCatchAll()boolstatus === 'catch_all'
isDisposable()boolstatus === 'disposable'
isDeliverable()boolvalid || risky || catch_all
costCredit()boolvalid || 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

AusnahmeHTTPWann
AuthenticationException401/403Ungültiger oder fehlender API-Schlüssel
RateLimitException42960 Anfragen/Min. oder stündliches Limit. $e->retryAfterSeconds
NoCreditsException429Guthaben 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

OrdnerBeschreibung
src/Client.phpHaupt-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)
email-validator-php.zip herunterladen Zurück zu den Add-ons

v5.0.0 · MIT-Lizenz · PHP 8.1+ · Guzzle 7 · PSR-6 / PSR-3