EmailAddressZend_Validate_EmailAddress allows you to validate an email address.
The validator first splits the email address on local-part @ hostname and attempts to match
these against known specifications for email addresses and hostnames.
Basic usage
A basic example of usage is below:
isValid($email)) {
// email appears to be valid
} else {
// email is invalid; print the reasons
foreach ($validator->getMessages() as $message) {
echo "$message\n";
}
}
]]>
This will match the email address $email and on failure populate
$validator->getMessages() with useful error messages.
Complex local partsZend_Validate_EmailAddress will match any valid email address
according to RFC2822. For example, valid emails include bob@domain.com,
bob+jones@domain.us, "bob@jones"@domain.com and
"bob jones"@domain.com
Some obsolete email formats will not currently validate (e.g. carriage returns or a
"\" character in an email address).
Validating different types of hostnames
The hostname part of an email address is validated against
Zend_Validate_Hostname. By default
only DNS hostnames of the form domain.com are accepted, though if you wish
you can accept IP addresses and Local hostnames too.
To do this you need to instantiate Zend_Validate_EmailAddress
passing a parameter to indicate the type of hostnames you want to accept. More details
are included in Zend_Validate_Hostname, though an example of how
to accept both DNS and Local hostnames appears below:
isValid($email)) {
// email appears to be valid
} else {
// email is invalid; print the reasons
foreach ($validator->getMessages() as $message) {
echo "$message\n";
}
}
]]>Checking if the hostname actually accepts email
Just because an email address is in the correct format, it doesn't necessarily mean
that email address actually exists. To help solve this problem, you can use MX
validation to check whether an MX (email) entry exists in the DNS record for the
email's hostname. This tells you that the hostname accepts email, but doesn't tell you
the exact email address itself is valid.
MX checking is not enabled by default and at this time is only supported by UNIX
platforms. To enable MX checking you can pass a second parameter to the
Zend_Validate_EmailAddress constructor.
Alternatively you can either pass TRUE or
FALSE to $validator->setValidateMx() to enable or
disable MX validation.
By enabling this setting network functions will be used to check for the presence of an
MX record on the hostname of the email address you wish to validate. Please be aware
this will likely slow your script down.
Validating International Domains NamesZend_Validate_EmailAddress will also match international
characters that exist in some domains. This is known as International Domain Name (IDN)
support. This is enabled by default, though you can disable this by changing the
setting via the internal Zend_Validate_Hostname object that
exists within Zend_Validate_EmailAddress.
hostnameValidator->setValidateIdn(false);
]]>
More information on the usage of setValidateIdn() appears in
the Zend_Validate_Hostname documentation.
Please note IDNs are only validated if you allow DNS hostnames to be validated.
Validating Top Level Domains
By default a hostname will be checked against a list of known TLDs. This is enabled by
default, though you can disable this by changing the setting via the internal
Zend_Validate_Hostname object that exists within
Zend_Validate_EmailAddress.
hostnameValidator->setValidateTld(false);
]]>
More information on the usage of setValidateTld() appears in
the Zend_Validate_Hostname documentation.
Please note TLDs are only validated if you allow DNS hostnames to be validated.
Setting messagesZend_Validate_EmailAddress makes also use of
Zend_Validate_Hostname to check the hostname part of a given
email address. As with Zend Framework 1.10 you can simply set messages for
Zend_Validate_Hostname from within
Zend_Validate_EmailAddress.
setMessages(array(Zend_Validate_Hostname::UNKNOWN_TLD => 'I don't know the TLD you gave'));
]]>
Before Zend Framework 1.10 you had to attach the messages to your own
Zend_Validate_Hostname, and then set this validator within
Zend_Validate_EmailAddress to get your own messages returned.