Skip to content

"Argument exceeds maximum length of 4096 bytes" while mail:account:sync for some accounts #12487

@debpa

Description

@debpa

Forum discussion: https://help.nextcloud.com/t/mail-sync-broken-for-mailbox-argument-exceeds-maximum-length-of-4096-bytes/240891

Steps to reproduce

  1. Take a mailbox with a large inbox that is highly fragmented.
  2. Run php ./occ mail:account:sync -vvv
  3. You get an exception (you can also see this in the log during cron imap:sync): "Message":"Sync failed for :INBOX: The mail server was unable to parse the contents of the mail message: Argument exceeds maximum length of 4096 bytes."
  4. Now you look in the Stalwart Log and see: 2026-02-26T08:02:54Z DEBUG IMAP error occurred (imap.error) listenerId = "imap", localPort = 143, remoteIp = , remotePort = 45630, details = "Argument exceeds maximum length of 4096 bytes.", id = "10", type = "BAD", code = "PARSE"
  5. This has to do with QRESYNC, which appears to be limited to 4096 bytes in Stalwart 0.15.5.

Expected behavior

Sync without errors

Actual behavior

nextcloud.log
Message":"Sync failed for :INBOX: The mail server was unable to parse the contents of the mail message: Argument exceeds maximum length of 4096 bytes.

stalwart.log
2026-02-26T08:02:54Z DEBUG IMAP error occurred (imap.error) listenerId = "imap", localPort = 143, remoteIp = , remotePort = 45630, details = "Argument exceeds maximum length of 4096 bytes.", id = "10", type = "BAD", code = "PARSE"

Mail app version

5.7.1

Nextcloud version

32.0.6

Mailserver or service

Stalwart Mailserver 0.15.5

Operating system

Ubuntu 24.04 LTS

PHP engine version

PHP 8.3

Nextcloud memory caching

memcache.local, memcache.distributed, memcache.locking

Web server

Nginx

Database

MariaDB

Additional info

The reason is a false assumption in the
apps/mail/vendor/bytestream/horde-imap-client/lib/Horde/Imap/Client/Socket.php

1185 /* Check for extra long UID string. Assume that any
1186 * server that can handle QRESYNC can also handle long
1187 * input strings (at least 8 KB), so 7 KB is as good as
1188 * any guess as to an upper limit. If this occurs, provide
1189 * a range string (min -> max) instead. */

Unfortunately, this is not the case with the Stalwart mail server (at least in version 0.15.5). There, the limit is 4096 bytes.

The fixed value:

1190 if (strlen($uid_str = $uids->tostring_sort) > 7000) {

Corrected:

1190 if (strlen($uid_str = $uids->tostring_sort) > 4000) {

Or make the value configurable.
Or perhaps there is also a technical option to ask the IMAP server what it can do. But I don't know that.

Metadata

Metadata

Assignees

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions