This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Add a method to move messages to IMAPlib
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: frispete, mcepl
Priority: normal Keywords: patch

Created on 2018-04-21 19:10 by mcepl, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 19072 open mcepl, 2020-03-19 14:45
Messages (6)
msg315578 - (view) Author: Matej Cepl (mcepl) * Date: 2018-04-21 19:10
I am in the process of writing script working with IMAP first time using Python 3 for it (unfortunately, most of servers where I run other code is so ancient that even Python 2.7 is a stretch), and it is really nice experience so far. Many problems which are dealt with on the StackExchange with arcane workarounds are now resolved in email or imaplib libraries. Thank you, everybody!

However, it seems to me that few higher level commands could greatly improve useability of imaplib library. For example, moving messages (not fault of imaplib) is still horrible mess. In the end I had to write this monstrosity just to make moving messages working:

    Capas = collections.namedtuple('Capas', ['MOVE', 'UIDPLUS'])

    def __login(self, host='localhost', username=None, password=None, ssl=None): = imaplib.IMAP4_SSL(host=host)
        ok, data =, password)
        if ok != 'OK':
            raise IOError('Cannot login with credentials %s' %
                          str((host, username, password,)))

        ok, data = box.capability()
        capas = data[0].decode()
        box.features_present = Capas._make(['MOVE' in capas, 'UIDPLUS' in capas])

    def move_messages(self, target, messages):
            ok, data ='MOVE', '%s %s' % (messages, target))
            if ok != 'OK':
                raise IOError('Cannot move messages to folder %s' % target)
            ok, data ='COPY', '%s %s' % (messages, target))
            if ok != 'OK':
                raise IOError('Cannot copy messages to folder %s' % target)
            ok, data ='STORE',
                                    r'+FLAGS.SILENT (\DELETED) %s' % messages)
            if ok != 'OK':
                raise IOError('Cannot delete messages.')
            ok, data ='EXPUNGE', messages)
            if ok != 'OK':
                raise IOError('Cannot expunge messages.')
            ok, data ='COPY', '%s %s' % (messages, target))
            if ok != 'OK':
                raise IOError('Cannot copy messages to folder %s' % target)
            ok, data ='STORE',
                                    r'+FLAGS.SILENT (\DELETED) %s' % messages)
            if ok != 'OK':
                raise IOError('Cannot delete messages.')

It would be nice if some capabilities detection (see issue 18921) was embedded into login method, and if some version of move_messages was included in imaplib itself.
msg321952 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-07-19 11:56
See also bpo-33336: [imaplib] MOVE is a legal command.
msg372955 - (view) Author: Hans-Peter Jansen (frispete) * Date: 2020-07-03 20:50
If I'm not mistaken, this is applied to the openSUSE TW version of Python. 
For some reason, this seems to not play well with <instance>.uid('move',...)
on a cyrus imap server (v2.4.19). Is that to be expected?

2020-07-03 18:04:05  INFO: [imap_reorg] move b'10399' from 2017-01-01 06:30:35+02:00 to INBOX.2017
Traceback (most recent call last):
  File "./", line 431, in <module>
  File "./", line 425, in main
    return process()
  File "./", line 133, in trace_and_call
    result = func(*args, **kwargs)
  File "./", line 358, in process
    ret |= reorg.run_expr(expr)
  File "./", line 345, in run_expr
    return method(*args)
  File "./", line 328, in yearly
    ret = self.imap.uid('move', uid, dest)
  File "/usr/lib64/python3.8/", line 881, in uid
    typ, dat = self._simple_command(name, command, *args)
  File "/usr/lib64/python3.8/", line 1205, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib64/python3.8/", line 1030, in _command_complete
    raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: UID command error: BAD [b'Unrecognized UID subcommand']
msg372966 - (view) Author: Matej Cepl (mcepl) * Date: 2020-07-03 22:00
1. no this has not been included anywhere, just in the unfinished PR on GitHub
2. only thing which I was fighting to get into Python (and I did) was but that’s another issue (without this whole discussion here would not be even possible), it is now part of the upstream Python
3. are you certain that the IMAP server in question supports MOVE command? (have you noticed all that business with CAPABILITIES and particularly the UID one?)
msg372967 - (view) Author: Matej Cepl (mcepl) * Date: 2020-07-03 22:01
Sorry, UIDPLUS capability.
msg402188 - (view) Author: Matej Cepl (mcepl) * Date: 2021-09-19 23:36
> Hans-Peter Jansen (frispete) 
> imaplib.error: UID command error: BAD [b'Unrecognized UID subcommand']

You missed the fix for
Date User Action Args
2022-04-11 14:58:59adminsetgithub: 77508
2021-09-19 23:36:53mceplsetmessages: + msg402188
2020-07-03 22:01:04mceplsetmessages: + msg372967
2020-07-03 22:00:12mceplsetmessages: + msg372966
2020-07-03 20:50:56frispetesetnosy: + frispete
messages: + msg372955
2020-03-19 18:44:09vstinnersetnosy: - vstinner
2020-03-19 14:45:47mceplsetkeywords: + patch
stage: test needed -> patch review
pull_requests: + pull_request18427
2018-07-19 11:56:39vstinnersetnosy: + vstinner
messages: + msg321952
2018-04-27 18:20:02terry.reedysetstage: test needed
versions: - Python 2.7, Python 3.4, Python 3.5, Python 3.6, Python 3.7
2018-04-21 19:10:03mceplcreate