The DNSCheckValidation class performs a DNS lookup to ensure the domain part of an email address has valid DNS records (specifically A, AAAA, or MX records).
Key Behaviors:
- Reserved Domains: It automatically rejects domains using reserved TLDs (e.g.,
.test, .example, .invalid, .localhost) or private namespaces (e.g., .local, .intranet, .lan). - Record Requirements: It checks for the presence of
DNS_A, DNS_MX, or DNS_AAAA records. If no MX records are found, it falls back to checking A/AAAA records but will issue a NoDNSMXRecord warning. - Null MX Support: It respects RFC 7505; if a domain has a "Null MX" record (where the target is empty or
.), the email is considered invalid with the reason DomainAcceptsNoMail. - Requirements: This class requires the PHP
Intl extension to handle IDNA (Internationalized Domain Names in Applications) conversion.
use Egulias\
EmailValidator\\
Validation\\DNSCheckValidation;
use Egulias\\
EmailValidator\\
EmailValidator;
$validator = new EmailValidator();
$dnsValidation = new DNSCheckValidation();
// The validator will check if the domain in the email has valid DNS records
$isValid = $dnsValidation->isValid('user@example.com', $emailLexer);
if (!$isValid) {
$error = $dnsValidation->getError();
// Handle error (e.g., LocalOrReservedDomain, UnableToGetDNSRecord, etc.)
}
$warnings = $dnsValidation->getWarnings();