554 Commits

Author SHA1 Message Date
Rodolfo García Peñas (kix) 1f04bbd8bf folder/IMAP, repository/IMAP: make encoding conditional on utf_8_support
When utf_8_support is False (the default, standard RFC 3501 mode),
folder names received from the server are in Modified UTF-7 and must
be kept in that encoding internally.  When utf_8_support is True,
names are decoded to UTF-8 for internal use.

Previous code decoded unconditionally in IMAPFolder.__init__, which
would corrupt non-ASCII names received as Modified UTF-7 when
utf_8_support is False.  The inverse problem existed in
getfullIMAPname() and the three encode_mailbox_name() call sites in
IMAPRepository: they always converted UTF-8 → Modified UTF-7 before
sending to the server, which is wrong when utf_8_support is False
(names are already in Modified UTF-7 and must not be double-encoded).

Fix by applying the same conditional pattern consistently:

  if account.utf_8_support:
      name = imaputil.utf8_IMAP(name)   # UTF-8 → Modified UTF-7
  return imaputil.foldername_to_imapname(name)

This is applied in:
  - IMAPFolder.__init__       (decode on receive)
  - IMAPFolder.getfullIMAPname (encode before SELECT)
  - IMAPRepository.getfolders  (folderincludes SELECT)
  - IMAPRepository.deletefolder
  - IMAPRepository.makefolder_single

encode_mailbox_name() (which always assumed UTF-8 input) is removed
as it is no longer used anywhere.

Based on patch by Etienne Buira <etienne.buira@free.fr>
2026-05-17 11:22:35 +02:00
Rodolfo García Peñas (kix) 61291de09d Merge branch 'pr-247' into testing 2026-05-16 10:32:57 +02:00
Andreas Schacker a81e267ce5 Fix typos 2026-05-15 22:27:23 +02:00
Rodolfo García Peñas (kix) 15cadd7545 Refactor IMAP folder handling to use encode_mailbox_name for folder name encoding
This commit refactors the IMAP folder handling in the offlineimap repository to use the `encode_mailbox_name` function for encoding folder names. This change ensures that folder names are properly encoded when interacting with the IMAP server, improving compatibility and reliability when dealing with folder names that may contain special characters or non-ASCII characters. The `encode_mailbox_name` function combines UTF-8 encoding and quoting as needed, providing a consistent way to handle folder names across the codebase.
2026-05-15 09:51:11 +02:00
Rodolfo García Peñas (kix) 0185b11255 Fix UnicodeEncodeError on emails with malformed bytes
When fetching emails with defects or malformed bytes (e.g., spam or broken
MIME boundaries), the Python 3 `email.parser` handles un-decodable bytes
by safely substituting them with the Unicode Replacement Character `\ufffd`
(using the `errors='replace'` handler by default).

However, a problem arises during the `_fetch_from_imap` sync process when
OfflineIMAP3 tests if the message can be serialized back to bytes via
`as_bytes()`. If the malformed text part was originally declared as
`us-ascii` (or left unspecified, defaulting to ASCII), Python attempts to
encode the `\ufffd` string back to bytes using the ASCII codec. Since `\ufffd`
is out of the ASCII range, this triggers a `UnicodeEncodeError`, causing
OfflineIMAP3 to raise an `OfflineImapError` and entirely skip syncing the
message.

This commit fixes the issue by catching the `UnicodeEncodeError` when
`as_bytes()` fails. It then walks through the message parts, identifies
the text payloads that cannot be encoded with their current charset, and
dynamically forces their charset to `utf-8`. This allows Python to safely
encode the `\ufffd` character (automatically applying base64/quoted-printable
transfer encoding if needed), successfully serializing the message so it
can be synced without crashing.

Closes #240
Closes #229
Closes #224
Closes #160
2026-04-18 23:10:29 +02:00
serge-sans-paille 9c352d7d9c Always pass an exception as first argument of ui.error
In various cases we were passing string messages, which is incompatible
with how ui.error handles its argument, leading to bugs like #233

Fix #233

Signed-off-by: serge-sans-paille <sergesanspaille@free.fr>
2026-04-01 08:05:12 +02:00
Rodolfo García Peñas (kix) 47f74c4408 Handle bare exceptions thrown while getting message ID
This patch removes the bare exception, as proposed by Florian Snow.
See PR170.

Closes #119
2024-08-24 15:47:58 +02:00
Rodolfo García Peñas (kix) 38114ed24f Merge pull request #194 from lilydjwg/master
Fix SyntaxWarning & DeprecatedWarning
2024-08-15 15:28:27 +02:00
lilydjwg 489ff3bdb1 fix regexes containing invalid escape sequences
Signed-off-by: lilydjwg <lilydjwg@gmail.com>
2024-04-28 17:07:51 +08:00
Robo Shimmer c252df5590 ignore broken message-id - catch the exception 2024-01-15 22:42:18 +01:00
Kirill A. Korinsky 85e093caeb Fix support of UTF-8 inside email
Introduced by https://github.com/OfflineIMAP/offlineimap3/pull/56

Closes: https://github.com/OfflineIMAP/offlineimap3/issues/107
2023-05-02 00:23:58 +02:00
Rodolfo García Peñas (kix) da5d0641c5 Merge pull request #135 from me-and/fix-date-exception-handling
Skip parsing date after failing to parse the date
2022-12-05 11:39:44 +01:00
0pointerexception 7cd32cf834 LocalStatusSQLite.py: Python 3.11 compatible threadsafety check
Signed-off-by: 0pointerexception <w-gh@wmeyer.eu>
2022-11-17 19:47:57 +01:00
Adam Dinwoodie 416df5d7c2 Skip parsing date after failing to parse the date
When file_use_mail_timestamp or utime_from_header are enabled,
OfflineIMAP tries to parse the Date header in the email.  If the header
is present but invalid -- it doesn't contain a valid date -- this will
cause email.message to raise an exception.  This is all fine.  However
when handling that exception, OfflineIMAP can't try to extract the date
again: it's clearly invalid, and raising the same exception a second
time while handling the first exception just causes the entire sync to
fail.

To avoid that happening, don't try to provide the invalid date string in
the error message.  Instead, just give the user the UID of the email
that triggered the exception, and the exception text.

Ideally we'd instead fix the code to actually extract the header value
and provide it in the error message, but Python's email.message module
doesn't provide an easy way to get the raw text of the Date header from
an EmailMessage object; it's possible using private variables like
EmailMessage._headers, or by parsing the email using a custom
email.policy.EmailPolicy object that disables the module's attempts to
coerce the header value to a DateTime.  However, a user should be able
to get the problematic Date header from the message directly anyway, so
it's not worth adding all that complexity for something that should be
rare and provides little value.

Fixes #134

Signed-off-by: Adam Dinwoodie <adam@dinwoodie.org>
2022-08-25 16:26:57 +01:00
alessio-pascolini 210c23bdee Update IMAP.py
Return before decoding items of imapdata, None cannot be decoded.
2022-03-08 10:22:11 +01:00
Rodolfo García Peñas (kix) 1a0c29a00e OS rename raises an exception on Windows
This patch checks the exception raises by os.rename()
on Windows and provide the same behavior than Linux.

This patch is related to issue #37, issue 5.
This patch sets closes the issue 37.
closes #37
2021-07-25 15:53:09 +02:00
Rodolfo García Peñas (kix) c305f47e84 OSError does not include __getitem__
This patch removes the issue about using [] with
OSError instances. This es an old method from Python2.

This patch is related to issue #37, issue 4.
2021-07-25 15:53:09 +02:00
Rodolfo García Peñas (kix) fe443e6bd3 Merge pull request #72 from jishac/encoding_edge_cases
Encoding: edge cases and error handling
2021-06-09 06:03:54 +02:00
Joseph Ishac 9e0fb59bdf Adding missing import of NoBoundaryInMultipartDefect 2021-06-07 22:18:31 -04:00
Joseph Ishac 5b976fc5da Making error handling when parsing messages consistent 2021-06-07 21:57:54 -04:00
Joseph Ishac 9bebcbe4f7 Adding utilities to Base class.
Moving the quoted boundary fix to the Base class so that it can be used
by any subclass that needs to read an email.  Adding another utility to
extract message-id from a raw email.
2021-06-07 20:26:26 -04:00
Joseph Ishac b4f100c92c Merge branch to add extra debugging to imap parsing into encoding_edge_cases branch 2021-06-07 14:07:43 -04:00
Joseph Ishac b0aad911ab Adding some extra error handling when parsing from imaplib 2021-06-07 14:05:07 -04:00
Rodolfo García Peñas (kix) f6848015ff Merge pull request #55 from sudipm-mukherjee/gmail
BUG: Gmail FETCH error with synclabels enabled
2021-05-09 11:24:03 +02:00
Joseph Ishac 84b96e1daa Adding re.DOTALL to catch potentially folded lines and fixed formatting 2021-04-14 17:08:16 -04:00
Joseph Ishac a4532294ae Correcting an issue where dbg_output was not defined when the server was
unreachable due to an optimization in PR#56.  Since message-id is more
useful to better pin point the correct message, removing dbg_output.

Also fixing https://github.com/OfflineIMAP/offlineimap3/issues/62 by
correcting broken multipart boundaries or raising an error if as_bytes()
fails.  Related python bug submitted: https://bugs.python.org/issue43818
although this workaround should be sufficent in the interim.

Signed-off-by: Joseph Ishac <jishac@nasa.gov>
2021-04-14 14:54:25 -04:00
Joseph Ishac f024bb9e4c Wrap bytes check in a wrapper to be a bit more efficient 2021-04-14 11:19:22 -04:00
Joseph Ishac 0345390aa1 Fixed oversight in regular expression (boundary cannot be empty) 2021-04-14 11:14:44 -04:00
Joseph Ishac b78af75064 Adding a handler to detect the unlikely edge case where a message may
have an improperly quoted boundary that can cause the python library to
fail to reproduce the original message with msg.as_bytes().  See:
https://bugs.python.org/issue43818 and
https://github.com/OfflineIMAP/offlineimap3/issues/62
2021-04-13 00:01:26 -04:00
Joseph Ishac 6a45eef3b5 Fixed a minor bug discovered if server is unreachable when debugging 2021-04-12 22:58:58 -04:00
Joseph Ishac 7e7349d8ed Final Testing of IMAP and MailDir complete. GMAIL should be patched as well and I reviewed the code several times. However, I cannot test it, testers wanted!
This commit: Minor bug fixes from testing

Should finalize implementation of enhancement #48
https://github.com/OfflineIMAP/offlineimap3/issues/48

And fix issues #43 and #44
https://github.com/OfflineIMAP/offlineimap3/issues/43
https://github.com/OfflineIMAP/offlineimap3/issues/44

Signed-off-by: Joseph Ishac <jishac@nasa.gov>
Tested-by: Joseph Ishac <jishac@nasa.gov>
2021-02-23 20:12:55 -05:00
Joseph Ishac 259bf83607 Fixing up the rest of the parsing and IMAP functions, and GMAIL classes as well. Also adding is_debugging() to the UI to allow us to quickly determine if we should build some of the expensive debug objects 2021-02-23 16:17:54 -05:00
Joseph Ishac 5fc08e529b BUG: behavior of fetch now only returns a single entry, the use of
data[0][0] here is a carry over from the old behavior of offlineimap
(python2)
2021-02-22 23:27:45 -05:00
Joseph Ishac 3166723baa Removing obsolete emailutil.py. The date functionality was pulled into folder/Base.py which now handles messages as binary objects 2021-02-21 23:20:39 -05:00
Joseph Ishac 14b318b0e2 Formatting and adding the requirements that were removed as part of the "Included charset detection" patch that was reverted. Added all but chardet~=3.0.4 which is not needed with this fix. 2021-02-21 23:09:33 -05:00
Sudip Mukherjee 8b88441759 BUG: Gmail FETCH error with synclabels enabled
If synclabels is enabled then offlineimap is sending '1:*' to imaplib2,
and imaplib2 while creating the FETCH command is quoting the sequence
and the command becomes:
b"JFFJ10 FETCH '1:*' (FLAGS X-GM-LABELS UID)\r\n"

Remove the single-quotes to prevent that and also consider the response
as bytes.

Closes: #52

Signed-off-by: Sudip Mukherjee <sudipm.mukherjee@gmail.com>
2021-02-20 00:34:01 +00:00
Joseph Ishac 49b6c30ace Cleaning up some more use of the old message string "content" to use "msg" email object instead. 2021-02-19 17:00:15 -05:00
Rodolfo García Peñas (kix) 62490ff183 Included charset detection
This patch includes charset detection to read the message.

This patch is related to issue #43

Signed-off-by: Rodolfo García Peñas (kix) <kix@kix.es>
2021-02-19 16:39:17 +01:00
Joseph Ishac 1d2478bcb6 Series of *UNTESTED* changes that should move the internal structure of
a message from a string to an email object that is part of the built-in
email library.  The allows for emails to be processed as bytes and
re-encoded properly if they are not UTF-8 or ascii encoded.  Currently
these changes cover the Base, IMAP, and Maildir classes but not the
specialized GMAIL class yet.
2021-02-09 14:58:30 -05:00
Rodolfo García Peñas (kix) 0fe1caa6a5 Avoid crash in search if no results
I am incluiding this check to avoid crash if the array is empty
and we are trying to read the first element.
2020-11-08 22:07:14 +01:00
Rodolfo García Peñas (kix) a311faf183 IMAP search now works fine
This patch converts the search results from bytes to strings

I add a bit comment about it here:

    In Py2, with IMAP, imaplib2 returned a list of one element string.
      ['1, 2, 3, ...'] -> in Py3 is [b'1 2 3,...']
    In Py2, with Davmail, imaplib2 returned a list of strings.
      ['1', '2', '3', ...] -> in Py3 should be [b'1', b'2', b'3',...]

    In my tests with Py3, I get a list with one element: [b'1 2 3 ...']
    Then I convert the values to string and I get ['1 2 3 ...']

    With Davmail, it should be [b'1', b'2', b'3',...]
    When I convert the values to string, I get ['1', '2', '3',...]
2020-11-08 15:47:51 +01:00
Rodolfo García Peñas (kix) 5ccb89a412 BUG: Read response as string from APPENDUID
We need read the response from APPENUID and convert it to string.
This patch do it.
2020-11-07 16:48:09 +01:00
Rodolfo García Peñas (kix) 3f86218e55 IMAP.py split long lines
This patch split long lines (>=80 chars)
2020-11-07 15:25:27 +01:00
Rodolfo García Peñas (kix) f7534c70ce __init__.py added docstrings
Just added docstrings.
2020-11-01 13:12:03 +01:00
Rodolfo García Peñas (kix) f53bee7335 Base.py added docstrings
Just added docstrings.
2020-11-01 13:11:52 +01:00
Rodolfo García Peñas (kix) 9239c804e9 Base.py remove redundant parenthesis
Just remove these parenthesis
2020-10-31 16:55:58 +01:00
Rodolfo García Peñas (kix) 8504cdc6b8 Base.py long lines style
This patch changes long lines (>80 chars) to lines <80 chars.
The patch only changes the style.
2020-10-31 16:55:20 +01:00
Rodolfo García Peñas (kix) 442c88d838 imaplib expect bytes in the append
imaplib2 is doing this code for strings:

        if isinstance(message, str):
            message = bytes(message, 'ASCII')

But our message is already encoded using 'utf-8'.
Then, we can set the message as bytes, encoded using 'utf-8'
in offlineimap and imaplib2 won't change our message.

This patch solves this problem:

WARNING:OfflineImap:
Traceback:
  File "/home/kix/src/offlineimap3/offlineimap/folder/Base.py", line 1127, in syncmessagesto
    action(dstfolder, statusfolder)
  File "/home/kix/src/offlineimap3/offlineimap/folder/Base.py", line 955, in __syncmessagesto_copy
    self.copymessageto(uid, dstfolder, statusfolder, register=0)
  File "/home/kix/src/offlineimap3/offlineimap/folder/Base.py", line 855, in copymessageto
    new_uid = dstfolder.savemessage(uid, message, flags, rtime)
  File "/home/kix/src/offlineimap3/offlineimap/folder/IMAP.py", line 668, in savemessage
    (typ, dat) = imapobj.append(self.getfullIMAPname(),
  File "/usr/lib/python3/dist-packages/imaplib2.py", line 660, in append
    message = bytes(message, 'ASCII')
2020-10-25 20:36:07 +01:00
Thomas De Schampheleire 33e0efa163 IMAP: replace non-UTF-8 characters rather than aborting
Emails received may not be UTF-8. Following error was observed on a specific
mail:

Traceback (most recent call last):
  File "/home/tdescham/repo/offlineimap3/offlineimap/threadutil.py", line 146, in run
    Thread.run(self)
  File "/usr/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/tdescham/repo/offlineimap3/offlineimap/folder/Base.py", line 850, in copymessageto
    message = self.getmessage(uid)
  File "/home/tdescham/repo/offlineimap3/offlineimap/folder/IMAP.py", line 327, in getmessage
    data = self._fetch_from_imap(str(uid), self.retrycount)
  File "/home/tdescham/repo/offlineimap3/offlineimap/folder/IMAP.py", line 844, in _fetch_from_imap
    ndata1 = data[0][1].decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 10177: invalid start byte

This completely aborted offlineimap3, blocking further mail reception.

Instead, use the 'replace' error strategy in Python:

    Replace with a suitable replacement character; Python will use the
    official U+FFFD REPLACEMENT CHARACTER for the built-in Unicode codecs on
    decoding and ‘?’ on encoding.
    https://docs.python.org/2/library/codecs.html#codec-base-classes
2020-10-21 16:29:13 +02:00
Thomas De Schampheleire 820e5c855f IMAP: Python 3 bytes fix on first download of account
ERROR: ERROR in syncfolder for gmail folder INBOX: Traceback (most recent call last):
  File ".../offlineimap3/offlineimap/accounts.py", line 634, in syncfolder
    cachemessagelists_upto_date(maxage)
  File ".../offlineimap3/offlineimap/accounts.py", line 526, in cachemessagelists_upto_date
    min_date=time.gmtime(time.mktime(date) + 24 * 60 * 60))
  File ".../offlineimap3/offlineimap/folder/IMAP.py", line 277, in cachemessagelist
    imapobj, min_date=min_date, min_uid=min_uid)
  File ".../offlineimap3/offlineimap/folder/IMAP.py", line 259, in _msgs_to_fetch
    search_result = search(search_cond)
  File ".../offlineimap3/offlineimap/folder/IMAP.py", line 222, in search
    if ' ' in res_data[0] or res_data[0] == '':
TypeError: a bytes-like object is required, not 'str'
2020-10-21 14:28:23 +02:00