laminas/laminas-mail

Allow non-strict email address validation

michalbundyra opened this issue · 11 comments

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


Originally posted by @davidwindell at zendframework/zend-mail#83

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?


Originally posted by @steffenweber at zendframework/zend-mail#83 (comment)

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.


Originally posted by @kaero598 at zendframework/zend-mail#83 (comment)

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: user@example.org ().

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.


Originally posted by @DominikTo at zendframework/zend-mail#83 (comment)

Hello, I did solve it with Validator quick fix for Moon Box. Cf : Monwoo/MoonBox@0636a95#diff-1a8dca4bf22bff2976d4f1eac7e2f50f

Use composer Autoload feature to quick fix vendors files :
"autoload": { "files": [ "vendorFixies/zend-validator/EmailAddress.php" ] },
=> Monwoo/MoonBox@0636a95#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;
        }
...

=> Monwoo/MoonBox@0636a95#diff-54278b9d8fed58a67d9f6b35ad65321dR525


Originally posted by @Monwoo at zendframework/zend-mail#83 (comment)

Hello, I wanted to know if there are updates here, I'm having the same issue. I'm iterating through an inbox and getting some errors couse of parsing garbage, like
'in.hostname.it>' is not a valid hostname for the email address or The input exceeds the allowed length.
Is there a way to let laminas be less strict?

I guess this is duplicate of #146

@glensc: I think you meant this is duplicated by #146? Because how can an earlier ticket be a duplicate of a later one? ;-)

@svaningelgem there's no requirement to keep the oldest ticket alive, they duplicate each other, and as the #146 has the most meaningful content, I'd keep that open.

Also, the #64 issue content is from the year 2016, carried over from the previous zend-mail, unlikely the reporters be around to support additional questions or testing.

#146 has a solution on how the problem could be handled:

I guess nobody is motivated deeply enough to carry over the code from there to this repo.

+1 Facing this issue with Gmail