Skip to content

Detecting disposable / temporary email addresses in PHP

By Elliot J. Reed

Use the elliotjreed/disposable-emails-filter PHP package to detect temporary and disposable email addresses in your applications, preventing abuse in competitions, voucher systems, and newsletter signups.

Installation

The disposable-emails-filter package uses a curated list of disposable email domains maintained by Martin Cech at github.com/disposable-email-domains/disposable-email-domains.

Install the package via Composer:

Install
composer require elliotjreed/disposable-emails-filter

Basic Usage

Use the package to check if an email address is disposable:

Usage
<?php
require 'vendor/autoload.php';
use ElliotJReed\DisposableEmail\Email;
if ((new Email())->isDisposable('email@temporarymailaddress.com')) {
echo 'This is a disposable / temporary email address';
}

Handling Invalid Emails

If an invalid email address is provided, an InvalidEmailException is thrown. Always validate the email format first using PHP's built-in filter_var() function:

Handling invalid emails
$email = 'not-a-real-email-address#example.net'
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if ((new Email())->isDisposable($email)) {
echo 'This is a disposable / temporary email address';
}
} else {
echo 'This is not a valid email address';
}

Exception Handling with Try-Catch

For more robust error handling, especially in production applications, use a try-catch block to handle the InvalidEmailException:

Try-Catch Example
<?php
require 'vendor/autoload.php';
use ElliotJReed\DisposableEmail\Email;
use ElliotJReed\DisposableEmail\Exception\InvalidEmailException;
try {
$email = new Email();
if ($email->isDisposable('user@example.com')) {
echo 'This is a disposable email address';
} else {
echo 'This is a valid, non-disposable email address';
}
} catch (InvalidEmailException $exception) {
echo 'Invalid email address provided: ' . $exception->getMessage();
}

Package Information

The disposable-emails-filter package is actively maintained and compatible with PHP 7.4 and above, including PHP 8.x. The underlying disposable email domain list is regularly updated to include new temporary email services as they appear. The package uses semantic versioning and is production-ready for use in commercial applications.

Source code and contributions are available at github.com/elliotjreed/disposable-emails-filter-php.

Frequently Asked Questions

How often is the disposable email list updated?

The package uses the disposable-email-domains repository maintained by Martin Cech, which is updated regularly as new disposable email services are discovered. When you install or update the package via Composer, you'll get the latest version of the domain list. For the most current protection, keep your dependencies updated with composer update.

What if a legitimate domain is flagged?

Whilst false positives are rare, if a legitimate domain is incorrectly flagged as disposable, you have two options: (1) Report the issue to the upstream repository for review and correction, or (2) implement custom logic in your application to whitelist specific domains before checking with the package. Remember that the list is community-maintained and corrections are typically made quickly.

Can I whitelist specific domains?

The package itself doesn't provide a built-in whitelist feature, but you can easily implement this in your application logic. Before calling isDisposable(), check if the email domain is in your custom whitelist array. For example: if your organisation uses a domain that's incorrectly flagged, add it to an array of allowed domains and skip the disposable check for those addresses. This gives you flexibility whilst still benefiting from the package's comprehensive detection.