laminas / laminas-mail

Provides generalized functionality to compose and send both text and MIME-compliant multipart e-mail messages
https://docs.laminas.dev/laminas-mail/
BSD 3-Clause "New" or "Revised" License
95 stars 66 forks source link

Allow non-strict email address validation #64

Open michalbundyra opened 4 years ago

michalbundyra commented 4 years ago

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 https://github.com/zendframework/zend-mail/issues/83

michalbundyra commented 4 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?


Originally posted by @steffenweber at https://github.com/zendframework/zend-mail/issues/83#issuecomment-280024619

michalbundyra commented 4 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.


Originally posted by @kaero598 at https://github.com/zendframework/zend-mail/issues/83#issuecomment-288379741

michalbundyra commented 4 years ago

+1


Originally posted by @svaningelgem at https://github.com/zendframework/zend-mail/issues/83#issuecomment-376565845

michalbundyra commented 4 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.


Originally posted by @DominikTo at https://github.com/zendframework/zend-mail/issues/83#issuecomment-418774721

michalbundyra commented 4 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


Originally posted by @Monwoo at https://github.com/zendframework/zend-mail/issues/83#issuecomment-454492000

victorannunzi commented 3 years ago

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?

glensc commented 3 years ago

I guess this is duplicate of #146

svaningelgem commented 3 years ago

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

glensc commented 3 years ago

@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.

glensc commented 3 years ago

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.

abulhol commented 3 years ago

+1 Facing this issue with Gmail