简体   繁体   中英

Zend Framework hostname Email Validator fails on yahoo.gr

I am using zend framework to send emails. I have an Hostname::ALLOW_DNS validator. It fails when trying to send email to yahoo.gr. I get this error:

An error occurred
An error occurred during execution; please try again later.
Additional information:
Zend\Mail\Exception\InvalidArgumentException
File:
/var/www/file/project/vendor/zendframework/zendframework/library/Zend/Mail/Address.php:41
Message:
'yahoo.gr ' is not a valid hostname for the email address

The email is smth@yahoo.gr. Any suggestions?

From your error, the only problem I can see is the whitespace 'yahoo.gr ' which will/can cause failure of validation.

Fix Suggestion 1:

You should start using trim() on your GET/POST "email" value.

Fix Suggestion 2:

Which is by the way in the documentation :

Validating only the local part

If you need Zend\\Validator\\EmailAddress to check only the local part of an email address, and want to disable validation of the hostname, you can set the domain option to FALSE. This forces Zend\\Validator\\EmailAddress not to validate the hostname part of the email address.

$validator = new Zend\Validator\EmailAddress();
$validator->setOptions(array('domain' => FALSE));

Fix Suggestion 3:

Which is by the way in the documentation as well:

Validating different types of hostnames

The hostname part of an email address is validated against Zend\\Validator\\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\\Validator\\EmailAddress passing a parameter to indicate the type of hostnames you want to accept. More details are included in Zend\\Validator\\Hostname, though an example of how to accept both DNS and Local hostnames appears below:

$validator = new Zend\Validator\EmailAddress(
                    Zend\Validator\Hostname::ALLOW_DNS |
                    Zend\Validator\Hostname::ALLOW_LOCAL);
if ($validator->isValid($email)) {
    // email appears to be valid
} else {
    // email is invalid; print the reasons
    foreach ($validator->getMessages() as $message) {
        echo "$message\n";
    }
}

Well, no more suggestions. I wish you good luck!

The End.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM