This is me, Wu!
» e-shell.org
In this page
Rate this page!
Search inside the wiki!
  Home >> sources >> IMAP Folder backup tool

IMAP Folder backup tool

This is a little script to create a backup of an IMAP folder into a mbox file, you will be able to restore mails inside such backup using almost every mail user agent (MUA for short) avaliable today (sylpheed, evolution, eudora, thunderbird, etc).

Download: MboxDump?.py v0.1 (November 22, 2006) | tar.gz | tar.bz2 | zip |

Source:

#!/usr/bin/env python
# coding: iso-8859-15
#  -*- Python -*-

"""

 Script to create dumps from mailboxes in a given imap server

 Some parameters must be provided as arguments.

        $ MboxDump [-h host] [-P port] ][-u username] [-p passwd] [-m mbox] [-o output file] [-t dump type]

        -h host
           Set the name or IP address of the imap server we will connect to.
           If no host is set, localhost is assumed.

        -P port
           Set the port to connect, by default 143.

        -u username
           Set the username we will use to connect to the imap server.
           If no username is provided, the current username is used
           (the one of the user that runs MboxDump).

        -p passwd
           This sets the password we will use to connect to the imap server.
           If no password is provided, the script will ask for one.

        -m mbox
           With this parameter, we set which mailbox we want to dump. Default is INBOX.

        -o outputfile
           Sets the name of the file where the mbox dump will be saved. If no output filename
           is set, it will defaults to username.mbox (for example frank.INBOX)

        -t dump type
           Sets the format in which the dump will be made, by default unix mailbox format.
           The posibilities are:

           - mbox: Unix clasic mailbox format
           - maildir: maildir format.

 by Francisco de Borja L-bópez Río - sistemas@codigo23.net-A
 Soluciones Inform-báticas Có-Adigo23 S.L. - http://www.codigo23.net

"""

import getpass, imaplib, os, sys

def ShowHelp(args):

    """ Just show how to use the script and exit. """

    HelpString = """
MboxDump: Simple Python script to create dumps from cyrus imapd mailboxes

    Use:

        $ %s [-h host] [-P port] ][-u username] [-p passwd] [-m mbox] [-o output file] [-t dump type]

        -h host
           Set the name or IP address of the cyrus imapd server we will connect to.
           If no host is set, localhost is assumed.

        -P port
           Set the port to connect, by default 143.

        -u username
           Set the username we will use to connect to the cyrus imapd server.
           If no username is provided, the current username is used (the one of the user
           that runs %s).

        -p passwd
           This sets the password we will use to connect to the cyrus imapd server.
           If no password is provided, the script will ask for one.

        -m mbox
           With this parameter, we set which mailbox we want to dump. Default is INBOX.

        -o outputfile
           Sets the name of the file where the mbox dump will be saved. If no output filename
           is set, it will defaults to username.mbox (for example frank.INBOX)

        -t dump type
           Sets the format in which the dump will be made, by default unix mailbox format.
           The posibilities are:

           - mbox: Unix clasic mailbox format
           - maildir: maildir format.

    """ % (args[0], args[0])

    print HelpString


def MboxDump(hostname='localhost', hostport=143, username=getpass.getuser(), passwd='', mbox='INBOX', output_file='', dump_type='mbox'):

    """ Method to dump a mailbox """

    # If no output file is provided, we set the default one:
    if output_file == '':
        output_file = username + '.' + mbox

    try:
        # connect to the imap server
        imap_conn = imaplib.IMAP4(hostname, hostport)
    except:
        print 'Error: Could not connect to host %s on port %s ' %(hostname, hostport)
    else:
        try:
            # authenticate as username, passwd
            imap_conn.login(username, passwd)
        except:
            print 'Error: Could not login as user %s with provided password' % username
        else:
            try:
                # select the imap folder we will dump
                imap_conn.select(mbox)
            except:
                print 'Error: Could not open mailbox %s ' % mbox
            else:
                # get the number of e-mails inside the folder
                typ, data = imap_conn.search(None, 'ALL')
                if len(data) <= 0:
                    print 'Warning: mailbox %s is empty, no messages to dump' % mbox
                else:
                    # go through the list of mails and create the dump in mailbox format.
                    mboxdump = ''
                    for num in data[0].split():

                        # To build each entry from the imap folder, we need to get
                        # not only the RFC822 message, but some other information before:

                        typ, data = imap_conn.fetch(num, '(BODY[HEADER.FIELDS (FROM)])')

                        # Comment the following line if you are using python < 2.3.5
                        message = 'From ' + data[0][1].lstrip('From: ').strip() + ' '
                        # Uncomment the following if you are using python < 2.3.5
                        #message = 'From ' + data[0][1].split('From: ')[1].strip() + ' '

                        typ, data = imap_conn.fetch(num, '(INTERNALDATE)')
                        # we have to perform some pre-processing of the date
                        # first check if there is an internal date, if not, use a dummy one
                        if len(data) > 0:
                            procdatetime = data[0].split('"')[1].lstrip().split(' ')
                            procdate = procdatetime[0].split('-')
                            message = message + 'Mon ' + procdate[1] + '  ' + procdate[0] + ' ' + procdatetime[1] + ' ' + procdate[2] + '\n'
                        else:
                            message = message + 'Fri Sep  1 08:23:28 2006 \n'

                        #message = message + data[0].split('"')[1] + '\n'

                        typ, data = imap_conn.fetch(num, '(RFC822)')
                        message = message + data[0][1]

                        # once we get the mail information, we add it to the dump
                        mboxdump = mboxdump + message


                    # Once the dump is complete, we check if the information has to be displayed
                    # on-screen or saved into a file

                    openfile = open(output_file, 'w')
                    openfile.write(mboxdump)
                    openfile.close()


if __name__ == '__main__':

    if len(sys.argv) < 3:

        # No args, we have to show the use help
        ShowHelp(sys.argv)

    else:

        # Ok, let's dump some mailboxes.
        #
        # First we check which parameters the user is providing us, setting
        # the proper options.

        # Check the host
        if '-h' in sys.argv:
            # host provided by the user
            hostname = sys.argv[sys.argv.index('-h')+1]
        else:
            # no host, default is localhost
            hostname = 'localhost'

        # Check the host port
        if '-P' in sys.argv:
            # port provided by the user
            hostport = int(sys.argv[sys.argv.index('-P')+1])
        else:
            # no port, default is 143
            hostport = 143

        # Check the username
        if '-u' in sys.argv:
            # username provided by the user
            username = sys.argv[sys.argv.index('-u')+1]
        else:
            # no username, by default we will use the same username as the user
            # running the script.
            username = getpass.getuser()

        # Check the password
        if '-p' in sys.argv:
            # password provided by the user
            passwd = sys.argv[sys.argv.index('-p')+1]
        else:
            # no password, by default we ask for a password
            passwd = getpass.getpass()

        # Check the mailbox to dump
        if '-m' in sys.argv:
            # mailbox name provided by the user
            mbox = sys.argv[sys.argv.index('-m')+1]
        else:
            # no mailbox specified, default is INBOX
            mbox = 'INBOX'

        # Check the output filename
        if '-o' in sys.argv:
            # output filename
            output_file = sys.argv[sys.argv.index('-o')+1]
        else:
            # no output filename, default
            output_file = username + '.' + mbox

        # Check the type of the dump we will provide
        if '-t' in sys.argv:
            # dump type provided by the user
            dump_type = sys.argv[sys.argv.index('-t')+1]
            #dump_type must be one of the following:
            if dump_type not in ['mbox', 'maildir']:
                #dump type not valid, getting it back to unix standar mailbox
                #(anyway we tell the user there is a problem)
                print 'Error: %s is not a valid type for the dump, using default type (mbox) ' %(dump_type)
                dump_type = 'mbox'

        else:
            # no mailbox specified, default is mbox
            dump_type = 'mbox'


        # Call MboxDump
        MboxDump(hostname, hostport, username, passwd, mbox, output_file, dump_type)