zendframework / zend-mail

Mail component from Zend Framework
BSD 3-Clause "New" or "Revised" License
96 stars 111 forks source link

Allow non-strict email address validation #83

Open davidwindell opened 8 years ago

davidwindell commented 8 years ago

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

steffenweber commented 7 years ago

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?

kaero598 commented 7 years ago

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.

svaningelgem commented 6 years ago

+1

DominikTo commented 6 years ago

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.

Monwoo commented 5 years ago

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

michalbundyra commented 4 years ago

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.