zbateson / mail-mime-parser

An email parser written in PHP
https://mail-mime-parser.org/
BSD 2-Clause "Simplified" License
458 stars 58 forks source link

Correct way to work with multiparts #192

Open lorenzo80 opened 2 years ago

lorenzo80 commented 2 years ago

Hi, we are using version 2.2.1 to parse our incoming email.

No unsolvable issue so far, until we received an automatic email from our email system about an undeliverable message. Trying to parse it proved impossible, because a big chunk of the message doesn't get retrieved by parse(). A quick troubleshooting revealed that the message was a multipart.

The problem is: I never saw one before, and there is practically no indications about how to handle them that ?ve been able to find. I can retrieve with getAllParts() but after that I'm at a loss.

Example Undelivered Mail Returned to Sender.txt

The message is attached, I had to redact the addresses / domains / subject per our policy but the rest is intact. I can deliver it to you privately, if needed. I tried to do a supposedly easy task -> retrieve the string "ORIGINAL SUBJECT". After three hours of testing with getAllParts(), getPart(), Get Child() I'm still at the starting point.

I'm pretty sure I tunneled and I might be missing something easy and obvious. Could you please point me toward some documentation or post the necessary code?

Thanks in advance!

zbateson commented 2 years ago

Hi @lorenzo80 --

The 'headers' of the undelivered email are a separate 'email object', they're not part of the current one if that makes sense. It's an attached part that happens to be an email.

Basically, if you pass the body of that part to the parser you'll get a separate email object, e.g. $returned = $mmp->parse($child->getContentStream()); echo $returned->getHeaderValue('subject'); should do the trick...