PHP code example of pyzit / tempmail

1. Go to this page and download the library: Download pyzit/tempmail library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pyzit / tempmail example snippets


use Pyzit\TempMail\TempMailClient;

$client = new TempMailClient('YOUR_API_TOKEN');

$result = $client->check('[email protected]');

if ($result->isDisposable) {
    echo '❌ Disposable email — rejected';
} else {
    echo '✅ Looks clean — allowed';
}

> $client = new TempMailClient($_ENV['PYZIT_TOKEN']);
> 

$result = $client->check('[email protected]');

$result->email          // '[email protected]'
$result->isDisposable   // true
$result->status         // 'disposable'
$result->isClean()      // false  (convenience method, opposite of isDisposable)

$result = $client->detailed('[email protected]');

$result->riskLevel       // 'low'
$result->classification  // 'trusted'
$result->reputationScore // 100 (integer 0 to 100)
$result->shouldReject()  // false  (convenience method)

// DNS intelligence
$result->hasMx()                  // true
$result->details->hasMx           // true
$result->details->mxRecords       // ['mail1.pyzit.com', 'mail2.pyzit.com']
$result->details->hasSpf          // true
$result->details->spfRecord       // 'v=spf1 

$result = $client->bulk([
    '[email protected]',
    '[email protected]',
    '[email protected]',
]);

$result->total                 // 3
$result->status                // 'mixed'
$result->disposableCount       // 1
$result->privacyRelayCount     // 1
$result->flaggedDomains        // ['mailnator.com']

$result->cleanEmails()         // ['[email protected]', '[email protected]']
$result->disposableEmails()    // ['[email protected]']
$result->privacyRelayEmails()  // ['[email protected]']

use Pyzit\TempMail\TempMailClient;
use Pyzit\TempMail\Exceptions\{
    AuthenticationException,
    InsufficientScopeError,
    QuotaExceededError,
    BatchLimitExceededError,
    RateLimitException,
    ApiException,
    TimeoutException,
    PyzitException
};

$client = new TempMailClient('YOUR_TOKEN');

try {
    $result = $client->check('[email protected]');

} catch (AuthenticationException $e) {
    // token is wrong — fail fast, don't retry
    die('Check your PYZIT_TOKEN environment variable.');

} catch (InsufficientScopeError $e) {
    // token lacks PI too slow — decide how to handle in your app
    echo 'Pyzit API timed out — allowing email through.';

} catch (ApiException $e) {
    // unexpected server error
    echo "Unexpected error {$e->getStatusCode()}: {$e->getResponseBody()}";

} catch (PyzitException $e) {
    // fallback for any other SDK error
    echo 'Something went wrong with the Pyzit SDK.';
}

function isAllowed(string $email): bool {
    try {
        return !$client->check($email)->isDisposable;
    } catch (PyzitException $e) {
        return true; // fail open — let the email through if API is down
    }
}

// app/Services/EmailValidator.php
namespace App\Services;

use Pyzit\TempMail\TempMailClient;
use Pyzit\TempMail\Exceptions\PyzitException;

class EmailValidator
{
    private TempMailClient $client;

    public function __construct()
    {
        $this->client = new TempMailClient(env('PYZIT_TOKEN'));
    }

    public function validate(string $email): bool
    {
        try {
            $result = $this->client->check($email);
            return !$result->isDisposable;
        } catch (PyzitException $e) {
            return true; // fail open
        }
    }
}

// app/Http/Controllers/AuthController.php
public function register(Request $request, EmailValidator $validator)
{
    if (!$validator->validate($request->email)) {
        return response()->json([
            'error' => 'Disposable email addresses are not allowed.'
        ], 422);
    }
    
    // continue with registration...
}

// src/Validator/NotDisposableEmail.php
namespace App\Validator;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Pyzit\TempMail\TempMailClient;
use Pyzit\TempMail\Exceptions\PyzitException;

class NotDisposableEmailValidator extends ConstraintValidator
{
    private TempMailClient $client;

    public function __construct()
    {
        $this->client = new TempMailClient($_ENV['PYZIT_TOKEN']);
    }

    public function validate($value, Constraint $constraint): void
    {
        try {
            $result = $this->client->check((string)$value);
            if ($result->isDisposable) {
                $this->context->buildViolation('Disposable emails are not allowed.')
                    ->addViolation();
            }
        } catch (PyzitException $e) {
            // fail open if API is unreachable
        }
    }
}

// src/Validator/NotDisposableEmail.php (constraint class)
namespace App\Validator;

use Symfony\Component\Validator\Constraint;

#[\Attribute]
class NotDisposableEmail extends Constraint
{
    public string $message = 'Disposable email addresses are not allowed.';
}

// functions.php
ail\TempMailClient;
use Pyzit\TempMail\Exceptions\PyzitException;

function validate_disposable_email($email) {
    $client = new TempMailClient(getenv('PYZIT_TOKEN'));
    
    try {
        $result = $client->check($email);
        return !$result->isDisposable;
    } catch (PyzitException $e) {
        return true; // fail open
    }
}

add_filter('registration_errors', function($errors, $sanitized_user_login, $user_email) {
    if (!validate_disposable_email($user_email)) {
        $errors->add('disposable_email', 'Disposable email addresses are not allowed.');
    }
    return $errors;
}, 10, 3);

$client = new TempMailClient(
    apiKey: 'YOUR_TOKEN',          // timeout: 5,                     // seconds, default 5
    maxRetries: 2,                  // retry attempts for 429/5xx, default 2
    failOpen: false,                // return clean on network errors, default false
);