Skip to content

Fix: IMAP Header Array Handling, Multipart Boundary Fallback, and Robust Date Parsing#152

Open
fsdultra wants to merge 3 commits intoZ-Hub:developfrom
fsdultra:develop
Open

Fix: IMAP Header Array Handling, Multipart Boundary Fallback, and Robust Date Parsing#152
fsdultra wants to merge 3 commits intoZ-Hub:developfrom
fsdultra:develop

Conversation

@fsdultra
Copy link
Copy Markdown

Released under the GNU Affero General Public License (AGPL), version 3
Released under the GNU Affero General Public License (AGPL), version 3 and Trademark Additional Terms.


What does this implement/fix?

This pull request addresses multiple issues related to header handling, multipart messages, and date parsing in the IMAP backend.


✅ Fix: Handle header fields as arrays in IMAP backend and address parser

  • Adjusted the handling of "date", "subject", and "from" headers in BackendIMAP to support array values (safely fallback to the first element when necessary).
  • Ensured Mail_RFC822 address parser uses the first element of the address array to avoid invalid parsing.
  • Prevents fatal errors caused by type mismatches when multi-valued headers are processed.

✅ Fix: Handle missing boundary in multipart messages

  • Previously, mimeDecode would fail when a multipart/* message lacked a boundary parameter.
  • This fix treats such cases as plain text, ensuring content is still parsed and decoded when requested.
  • Prevents silent message loss and improves compatibility with malformed emails.

✅ Improve date parsing in TimezoneUtil::MakeUTCDate

  • Added support for the ISO format 'Y-m-d\THis' (e.g., 2025-03-27T130000).
  • Returns false explicitly when all parsing attempts fail and logs an error to assist with debugging.

Does this close any currently open issues?


Any relevant logs, error output, etc?

Error 1 – Header as array:

[FATAL] [xxx@domain.com] Uncaught TypeError: strlen(): Argument #1 ($str) must be of type string, array given
in /usr/local/lib/z-push/lib/core/streamer.php:309

Error 2 – Invalid DateTime parsing:

[FATAL] [xxx@domain.com] Uncaught TypeError: date_timestamp_get(): Argument #1 ($object) must be of type DateTimeInterface, false given
in /usr/local/lib/z-push/lib/utils/timezoneutil.php:1335


Where has this been tested?

Sample messages (for reproduction):
📎 bugs.zip

Test Environment:

  • OS: Ubuntu 22.04
  • PHP Version: 8.2
  • Backend: Mail-in-a-Box (v72)

Client:

  • Device: PC
  • OS: Windows 10
  • Mail App: MS Outlook 2019

fsdultra added 3 commits June 16, 2025 22:48
Added support for 'Y-m-d\THis' format (e.g., 2025-03-27T130000).
Also ensures the method returns `false` explicitly when all parsing attempts fail and an error is logged.
Previously, if a multipart content-type (e.g. multipart/mixed) was missing a boundary parameter, mimeDecode would return an error and skip processing the part.
This commit changes the behavior to fallback to treating the content as plain text when no boundary is found. It ensures the body is included and decoded if requested, preventing silent data loss and improving compatibility with malformed messages
- Adjusted handling of "date", "subject", and "from" headers in BackendIMAP to support array values (take first element when needed).
- Ensured `Mail_RFC822` uses the first element of the address array to avoid invalid parsing.
- Prevents potential issues with multi-valued headers causing type mismatches or incorrect field extraction.
Copy link
Copy Markdown

@sevrinrobstad sevrinrobstad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks sane.

@7dotfabricio
Copy link
Copy Markdown

tks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Z-Push sync breaks when a message has improperly declared encoding

3 participants