php-mime-mail-parser / php-mime-mail-parser

A fully tested email parser for PHP 8.0+ (mailparse extension wrapper).
https://mailcare.io
MIT License
903 stars 196 forks source link

issue with attachments and getContent() for zero sized files #349

Open slotone opened 4 years ago

slotone commented 4 years ago

When receiving an email with a zero sized attachment, the method getContent() throws the following message: Warning: fseek(): supplied resource is not a valid stream resource

What's the proper way to detect zero sized attachments and avoid getContent() warning ?

Thanks in advance!

eXorus commented 4 years ago

Hello,

Could you attach to your issue a raw email with a zero sized attachment?

slotone commented 3 years ago

Hi Vincent,

After some testing we found the issue ocurrs with some attachments that are not being properly processed and therefore their result size is zero.

So far this issue seems to be only related to EML attachment files, so please find the sample.txt file we provide for you to test. The attached file is a raw mime email having an EML attachment inside (which is the one that is not being properly processed).

One error I've seen during this tests is: stream_get_contents(): supplied resource is not a valid stream resource [file] => /php-mime-mail-parser/src/Attachment.php [line] => 210

sample.txt Email addresses, domains and names were modified to preserve privacy.

This issue (EML attachment files not being parsed) happens frequently, but we didn't find a pattern so far. Worth to mention that EML Viewer in Gmail is able to properly display the EML attachment.

Please let us know if this helps to find a fix for it.

Thanks in advance!

eXorus commented 3 years ago

Sorry I didn't have time to look at it. I plan to do it before the end of the year ;)

Thanks for your issue.

pstast commented 2 years ago

I also experienced this problem and found a cause.

  1. Attachment::create() - when attachment does not have filename, it calls Parser::fromStream() with stream as parameter https://github.com/php-mime-mail-parser/php-mime-mail-parser/blob/e6b62963999fc9b5676a5bad0bc8fcfd387ae564/src/Attachment.php#L75
  2. Parser::fromStream() does some his stuff, but unfortunately it closes the input stream that came in parameter https://github.com/php-mime-mail-parser/php-mime-mail-parser/blob/e6b62963999fc9b5676a5bad0bc8fcfd387ae564/src/Parser.php#L132
  3. Later when you try to call Attachment->getContent(), it tries to use the existing stream, which has been closed by Parser::fromStream() https://github.com/php-mime-mail-parser/php-mime-mail-parser/blob/e6b62963999fc9b5676a5bad0bc8fcfd387ae564/src/Attachment.php#L210
  4. stream_get_contents(): supplied resource is not a valid stream resource error message appears

I will create a PR to fix it.