Fix/emailcollector phpimap inreplyto oauth v22 #36851
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes a few issues in EmailCollector (Dolibarr v22) that can lead to unnecessary OAuth refresh calls, incorrect header parsing in PHPIMAP mode, and missed tracking/filters when some mail servers only provide In-Reply-To (and not References).
File changed:
emailcollector.class.php
Changes
OAuth2 (PHPIMAP) token refresh
Stop forcing refresh on every run ($expire = true).
Refresh only when the token is expired (or will expire within ~30 seconds) when getEndOfLife() is available.
PHPIMAP header decoding + Reply-To
Decode potentially encoded header fields (From, To, Cc, Bcc, Subject) using decodeSMTPSubject() in PHPIMAP mode.
Use the Reply-To header for $replytostring instead of mixing it with in_reply-to.
Tracking/filters: use References + In-Reply-To
When applying EmailCollector filters related to tracking signature, consider In-Reply-To in addition to References.
When building the list of references used to detect Dolibarr tracking IDs, also include In-Reply-To and normalize the list.
Avoid false “outgoing email” classification
When MAIL_FROM_EMAILS_TO_CONSIDER_SENDING is empty or contains empty values, ignore empty entries so it can’t match everything.
Why
Always refreshing OAuth tokens increases latency and can hit provider throttling/limits.
PHPIMAP can return encoded “From:” values; saving them undecoded reduces readability and makes grouping/filtering harder.
Some providers put the previous Message-ID in In-Reply-To but do not populate References. Using both increases the chance of matching track IDs and applying track-related filters correctly.
Empty values in MAIL_FROM_EMAILS_TO_CONSIDER_SENDING can lead to misclassification of incoming emails as outgoing.
How to test (manual)
OAuth refresh: Run the EmailCollector several times; it should no longer refresh the token every execution when the token is still valid.
Header decoding: Process an email with an encoded From:/Subject: (e.g. =?UTF-8?...?=); stored values should be human-readable.
Tracking fallback: Reply to a Dolibarr email where the provider only sets In-Reply-To; EmailCollector should not miss the tracking signature and related filters.
Notes on coding style
The patch is generated from the upstream v22 file and keeps the original indentation/tabs to avoid CI errors like “Line indented incorrectly”.