[Dovecot] list all emails from command line?

Will Yardley dovecot.org at veggiechinese.net
Tue Jun 3 04:22:01 UTC 2014

This seems like a pretty complicated (and time / labor intensive) way to
solve the problem. That said, if this is the way you want to approach
the problem, Python's imaplib is pretty good at doing this kind of

This may not format it exactly the way you want, but it should give you
a starting point. Lots of examples online.

I haven't played with it much, but I think there are some things that
will let you do some extended logging of IMAP commands from within
Dovecot - that might be a better way to figure out why the user's client
doesn't seem to be noticing the message in a timely manner.


#!/usr/bin/env python

import email
import imaplib


i = imaplib.IMAP4_SSL(IMAPHOST)
for folder in i.list()[1]:
    folder = folder.split(' "/" ')[1]
    print "**** %s ****" % folder
    typ, [msg_ids] = i.search(None, 'ALL')
    for num in msg_ids.split():
        typ, msg_data = i.fetch(num, '(BODY.PEEK[HEADER])')
        for response_part in msg_data:
            if isinstance(response_part, tuple):
                email_message = email.message_from_string(response_part[1])
                to = email.utils.parseaddr(email_message['to'])
                subject = email_message['Subject']
                msgid = email_message['Message-ID']
                sender = email.utils.parseaddr(email_message['From'])

                print "%-30s => %-30s %-30s (%s)" % (sender[1], to[1] + ':',
                       subject[0:30], msgid)

More information about the dovecot mailing list