zend-mail icon indicating copy to clipboard operation
zend-mail copied to clipboard

Allow non-strict email address validation

Open davidwindell opened this issue 8 years ago • 6 comments

Add ability to specify whether EmailAddressValidator used by AddressList should validate the email with the strict flag set or not.

davidwindell avatar Apr 20 '16 13:04 davidwindell

This would be very useful. I'm using Zend-Mail to parse emails that arrive in an inbox and therefore have no control over their header values. When calling Zend\Mail\Storage\Imap->getMessage() an Exception is thrown from deep inside the Zend Framework if the Return-Path header contains a long address:

Fatal error: Uncaught Zend\Mail\Exception\InvalidArgumentException: The input exceeds the allowed length in vendor/zendframework/zend-mail/src/Address.php:41
Stack trace:
#0 vendor/zendframework/zend-mail/src/AddressList.php(249): Zend\Mail\Address->__construct('...', NULL)
#1 vendor/zendframework/zend-mail/src/AddressList.php(35): Zend\Mail\AddressList->createAddress('...', NULL)
#2 vendor/zendframework/zend-mail/src/AddressList.php(113): Zend\Mail\AddressList->add('...', NULL)
#3 vendor/zendframework/zend-mail/src/Header/AbstractAddressList.php(87): Zend\Mail\AddressList->addFromString('...')
#4 vendor/zendframework/zend-mail/src/Headers.php(482): Zend\Mail\Header\AbstractAddressList::fromString('To: ...')
#5 vendor/zendframework/zend-mail/src/Headers.php(231): Zend\Mail\Headers->loadHeader('To: ...')
#6 vendor/zendframework/zend-mail/src/Headers.php(95): Zend\Mail\Headers->addHeaderLine('To: ...')
#7 vendor/zendframework/zend-mime/src/Decode.php(142): Zend\Mail\Headers::fromString('Return-Path: ...', '\n')
#8 vendor/zendframework/zend-mail/src/Storage/Part.php(112): Zend\Mime\Decode::splitMessage('Return-Path: ...', 'Return-Path: ...', '')
#9 vendor/zendframework/zend-mail/src/Storage/Message.php(54): Zend\Mail\Storage\Part->__construct(Array)
#10 vendor/zendframework/zend-mail/src/Storage/Imap.php(128): Zend\Mail\Storage\Message->__construct(Array)
#11 mycode.php(31): Zend\Mail\Storage\Imap->getMessage(18)

A quick and dirty solution would be to add some static method like setStrictMode to the EmailAddress class. Any better ideas?

steffenweber avatar Feb 15 '17 14:02 steffenweber

I completely agree with @steffenweber. We use Zend Mail along with Zend Mime to parse bounce reports. And those often contain something like Return-Path: <> which triggers exception in AddressList.

kaero598 avatar Mar 22 '17 12:03 kaero598

+1

svaningelgem avatar Mar 27 '18 15:03 svaningelgem

Similar issue here. When iterating over an inbox (where you don't have control over the messages in there), I am e.g. running into Uncaught Zend\Mail\Exception\InvalidArgumentException: 'example.org ()' is not a valid hostname for the email address in zendframework/zend-mail/src/Address.php:77.

In my case the To header contains this garbage: To: [email protected] ().

Is there a good way to work around this? I'd guess that almost every "real life" inbox contains mails with such unexpected headers, which makes it almost impossible to iterate over an inbox.

DominikTo avatar Sep 05 '18 15:09 DominikTo

Hello, I did solve it with Validator quick fix for Moon Box. Cf : https://github.com/Monwoo/MoonBox/commit/0636a95e88f3eebce202e42bed48b5617abe0a73#diff-1a8dca4bf22bff2976d4f1eac7e2f50f

Use composer Autoload feature to quick fix vendors files : "autoload": { "files": [ "vendorFixies/zend-validator/EmailAddress.php" ] }, => https://github.com/Monwoo/MoonBox/commit/0636a95e88f3eebce202e42bed48b5617abe0a73#diff-1a8dca4bf22bff2976d4f1eac7e2f50fL16

Then, allow validation on failing case ('<>' for my case) :

...
    public function isValid($value)
    {
        if (! is_string($value)) {
            $this->error(self::INVALID);
            return false;
        }
        if ('<>' === $value) {
            // https://github.com/zendframework/zend-mail/issues/83
            // + add to your composer :
            // https://stackoverflow.com/questions/28104574/strategy-to-override-a-class-in-a-library-installed-with-composer
            return true;
        }
...

=> https://github.com/Monwoo/MoonBox/commit/0636a95e88f3eebce202e42bed48b5617abe0a73#diff-54278b9d8fed58a67d9f6b35ad65321dR525

Monwoo avatar Jan 15 '19 18:01 Monwoo

This repository has been closed and moved to laminas/laminas-mail; a new issue has been opened at https://github.com/laminas/laminas-mail/issues/64.

michalbundyra avatar Jan 15 '20 19:01 michalbundyra