IMAP Tools

Updated: April 14, 2012

Welcome to the IMAP Tools Website!            Rick Sanders            Subscribe to IMAP-Tools mailing list

Over the years I have written a number of tools and scripts for use with IMAP dedicated servers (what is IMAP? ). The IMAP Tools allow you to easily do such things such as copying user mailboxes from one IMAP server to another, synchronizing accounts on two servers, mass-migrating hundreds or thousands of users, purging duplicate messages, backing up and restoring IMAP accounts, loading messages from Maildir and Mbox servers into IMAP, and many others.

What do I get when I purchase the IMAP Tools set?
For $25 USD you get a package of 20 Perl scripts.


See the table below for a description of each tool and instructions on how to run it.

What kind of support comes with the IMAP Tools?
Support is provided by the developer on a time-available basis.

What if I am not happy with the IMAP Tools?
A 30 day Money-back refund is available.

How do I place an order?
Click on the "Buy Now" button below and you will be directed to the PayPal website where you can pay with your PayPal account or a credit card. After your payment has been received an e-mail will be sent to you with the imap_tools.tar.gz attachment containing all of the IMAP Tools.


  • Change History
  • SSL Support
  • IMAP debugging and tracing
  • Administrator login
  • Need a custom IMAP utility or help with an E-Mail Migration project?

    Name Description SSL ? Copies messages and mailboxes from one IMAP server to another. More info Yes Copies POP3 messages to an IMAP server. More info Yes Mass migration of messages for a set of users from one IMAP server to another. More info Yes Moves messages from one IMAP mailbox to another based on a set of rules. More info Yes Queries an IMAP server for a list of features it supports. More info Yes Writes IMAP messages to local files. More info Yes Loads messages from to a mailbox on an IMAP server. More info Yes Pings an IMAP server. More info Yes Synchronizes two IMAP accounts. More info Yes Copies messages from IMAP server to Mbox format. More info Yes Copies messages from an IMAP server to a user's Maildir. More info Yes Copies messages from Maildir to IMAP server. More info Yes Copies messages from Mbox format to IMAP server. More info Yes Synchronizes mbx and IMAP accounts. More info No Purges an IMAP mailbox. More info Yes Moves messages marked "Deleted" to trash mailbox. More info No Copies Mozilla messages to an IMAP server. More info No Deletes duplicate IMAP messages. More info Yes Deletes one or more mailboxes. More info Yes Displays a list of the user's folders. More info Yes

    More info:
      imapcopy is a utility for copying a user's messages from one IMAP server to another.   
      It logs into one server with your username & password, builds a list of your 
      mailboxes and the messages in them, then logs into the other server with your 
      username/password on that machine and copies the mailboxes and messages to it.  
      The mailbox (folder) structure is reproduced on the other server.  The IMAP status 
      flags are preserved (eg SEEN, DELETED, etc) along with the dates on the original messages.
      Usage: -S host1/user1/password1 -D host2/user2/password2    
      Optional arguments:                                               
         -d debug                                                      
         -v verbose (displays the progress of copying process)
         -I show IMAP protocol exchanges
         -L logfile                                                    
         -m <mailbox list> (copy only certain mailboxes, eg "INBOX,Drafts,etc")
         -r clears the Deleted flag from copied messages
         -x <delimiter [prefix]> Specify the src mbx delimiter and prefix (if server does not support NAMESPACE command)
         -y <delimiter [prefix]> Specify the dst mbx delimiter and prefix (if server does not support NAMESPACE command)
         -p <root mailbox> (put copied mailboxes under a root mailbox)
         -M <file of mailbox name mappings>
         -X <max size in MB>  Maximum message size to be copied 
         -U update mode.  Only copy messages that do not already exist on the destination.
         -B <msgnum start> Start copying messages from this number on.
         -E <msgnum end> Stop copying messages after this one.
         -a <DD-MMM-YYYY> Copy only those messages after the specifed date.
         -b <DD-MMM-YYYY> Copy only those messages before the specifed date.
      You can select a port number other than the standard IMAP port (143) by 
      specifying the host value as host:port, eg localhost:993.
      The -M argument allows you to change the names of mailboxes as they are 
      copied from the source to the destination system.  To do so, create a file of
      names in the format of "<source mailbox>: <destination mailbox>." For 
      Inbox: Migrated_inbox
      OldMail/2007/documents: OldMail/07/documents
      Sales: Old/sales/notes 
      Please note that when using the -M option if any mailbox names contain non-ASCII 
      characters you must install the Perl Unicode::IMAPUtf7 module.  The IMAP standard 
      requires that such mailbox names be encoded in the Modified UTF-7 character set.

    is a tool for copying a user's messages from a POP3  
      server to an IMAP4 server. makes a POP3 connection  
      to the POP3 host and logs in with the user's name and password.    
      It makes an IMAP connection to the IMAP host and logs in with the  
      user's IMAP username and password. then fetches     
      each message from the user's POP3 account and copies it to the     
      user's IMAP account.                                               
      The usernames and passwords are supplied via a text file specified 
      by the -i argument.  The format of the file of users is:           
      popUsername password imapUsername password    
      Usage: -p POP3host -i IMAPhost -u input_file
      Optional arguments:
          -n notify e-mail address
          -r delete POP3 message after copying to IMAP
          -t timeout in seconds
          -m <mailbox>  Default INBOX.  Specify as mbx1/mbx2/mbx3.
          -d debug mode
          -I show IMAP protocol exchanges
          -L logfile
          -R <range of messages, eg 200-333, to copy>
          -h print this message

    3. will copy messages from an IMAP server and place them under
      a directory the value of which you supply with the -f argument.  The
      name of each file is the message number associated with it in the IMAP
      server message store.  When the script finishes the file structure will
      look something like this:
            1 2 3 4 5 .....
            1 2
            1 2 3 4 5 6 7 8 9 a b c d e f 10 11 ...
      Usage:  -f /tmp/mailboxes  -S localhost/user/mypassword
      Some optional arguments are:
        -F <flags> where flags is a comma-separated list of either 
        standard or custom IMAP flags.  Messages with flags matching any 
        values in the list will dumped.
        -d debug
        -L <logfile>
        -m <mailbox list>  For example "INBOX,drafts,save/2003" will copy only the
      messages in the indicated folders.
        -a <DD-MMM-YYYY>  Dump only the messages which arrived after this date

    4. is a companion script to imapdump.  It will take a set of messages
      extracted from an IMAP server by imapdump (or some other utility which uses the 
      same format) and will load them into a mailbox on an IMAP server.
      ./                     \
           -i host/username/password      \
           -L dumptoIMAP.log              \
           -D /work/dump/Inbox            \
           [-m mbx1,mbx2,...,mbx3]
      The -D argument points to the directory where the message files are located.
      This directory should not have any files other than the messages.  The optional -m
      argument can be used to limit the mailboxes to be loaded rather than 
      to the list supplied rather than loading all of them.

      imapfilter allows you to scan folders on an IMAP server and move selected
      messages to different folders based on the contents of designated
      message header fields, eg Subject, From, Date, etc.
      imapfilter also supports the use of regular expressions in the
      matching rules.  For instance, the following rule will move
      all messages from a sender named "Jack" from the inbox to
      the wastebasket.
      From     ^Jack*   Inbox  Wastebasket
      Some examples of filter rules:
      #  Header field  match  source-mailbox   destination-mailbox.
      #  Note that mailboxes are case-sensitive but filter criteria are not.
      Subject  test$   Inbox Junk
      Importance High junk Inbox
      From  * Inbox Wastebasket
      X-Priority 3 Inbox Wastebasket
      Size  >50000 Inbox Big_Messages
      Usage: imapfilter -S host/rfs/mypassword -r rules.dat -L imapfilter.log
      Optionally the destination mailbox can belong to another user on the local server
      or on a remote IMAP server.  In the following two examples the first rule moves
      messages to a local mailbox belonging to another user while the second moves
      messages to a remote IMAP server.
      Subject *home*     INBOX  localhost:INBOX 
      Subject *testing*  INBOX
      Access control info must be supplied in the rules file as shown below.  Note that
      you can specify the IMAP port of the remote server if it is different than the
      standard 143.
      RemoteServer: localhost/user1/password1
      Wild-card mailboxes.  You can specify a '*' character in place of the local
      mailbox name in a rule.  This causes imapfilter to search the messages in each mailbox
      for matches.  For example:
      Subject *sales*     *   filtered_messages
      Notes on Date comparision operations.  imapfilter permits you     
      to filter on dates which are earlier, later, or the same as a    
      specified date.  The date in the rules must be in RFC822 Mail   
      date format (eg, >12 Nov 2009 12:45:10 +0500 means messages whose
      date is more recent that 12 Nov 2009 12:45:10 +0500) or expressed as an  
      offset from the current date (eg >30 meaning within the past 30  
      days and <30 meaning more than 30 days old).  Some examples:                                            
        Date    ">22 Dec 2008 15:00:00 +0000"      INBOX   MOVED      
        Date    "<15 Jan 2009 00:00:00 +0500"      INBOX   MOVED      
        Date    "=25 Dec 2009 08:00:00 +0500"      INBOX   MOVED      
        Date    ">60"  INBOX   OLD                                 
        Date    "*2009*"  INBOX   2009_messages                              
      Also please note that in order to use the date comparison feature you must install
      the DateTime, DateTime::Format::Mail and DateTime::Format::DateParse Perl
      The -f argument ("first match") causes imapfilter to honor the first rule that matches
      and to ignore any subsequent matches.  Without -f the last matching rule is the
      one that gets applied.

      imapPing is a tool for getting a rough feel for the response times
      from an IMAP server.  It connects to the server and then performs
      some basic IMAP operations on a user's  account and displays the
      time as each one is executed.  The operations are:
        1.  Connect to the IMAP server
        2.  Log in with a username and password
        3.  Get a list of mailboxes
        4.  Select the INBOX
        5.  Get a list of messages in the INBOX
        6.  Log off the server
      Usage: -h -u rfs9999 -p XXXXX

      imapsync is a tool for synchronizing user accounts on a pair of
      IMAP servers called the "source" and "destination" hosts.   
      You supply it with the host/userid/password on each host (and 
      optionally a list of mailboxes).  imapsync does the following:
      1.  Adds any messages on the 1st host which aren't on the 2nd 
      2.  Deletes any messages from the 2nd which aren't on the 1st 
      3.  Sets the message flags on the 2nd to match the 1st's flags
      imapsync works like this:
      1.  Log into the source host using the user name and password
      2.  Get a list of all mailboxes belonging to the user and a 
          list of messages in each mailbox.
      3.  Do the same for the destination host
      4.  For each message on the source see if the message exists on 
          the destination.  If not, add it to the destination.
      5.  For each message on the destination see if the message exists 
          on the source.  If not, delete it from the destination (eg, 
          set the \Deleted flag and expunge the mailbox).
      6.  For each message on the source compare the message flags with 
          the message flags on the destination.  If they don't match 
          update the flags on the destination.
      Some notes:
      a.  By default all of the user's mailboxes are processed but you 
          can limit that by providing a list of mailboxes in which case 
          only those will be examined (eg -m "Inbox, Drafts, Actions").
      b.  The Message-Id is used for identifying messages in an IMAP account.  
          A consequence of this is that if you have multiple copies of a 
          message in a source mailbox only one copy will be reproduced on 
          the destination host.
      Usage: imapsync -S sourcehost/username/password -D desthost/username/password
             imapsync -S sourcehost -D desthost -u <users file>
      Other (optional) arguments are:
        -L logfile  (if omitted the output goes to STDOUT)
        -u <file of users>  Format srcUser:srcPwd:dstUser:dstPwd
        -d debug mode
        -m mailbox list (eg "Inbox, Drafts, Actions")
         -x <delimiter [prefix]> Specify the src mbx delimiter and prefix (if server does not support NAMESPACE command)
         -y <delimiter [prefix]> Specify the dst mbx delimiter and prefix (if server does not support NAMESPACE command)

      IMAPtoMbox will copy mailboxes and messages from an IMAP server to the local
      server (or PC) in the Unix mbox format.  The mailbox hierarchy (nested mailboxes)
      is preserved.
      To run IMAPtoMbox you supply the name of the IMAP server, your username & password 
      along with a pointer to the directory on the local host where the mailboxes are to be 
      created.  The local mailbox is given the same name as the IMAP mailbox.  For example:
      Usage: IMAPtoMbox -i server/user/pwd -m mailbox_directory
      Optional arguments:
      -L logfile
      For example:
      ./IMAPtoMbox                                      \
              -i   \
              -m  /home/rfs                             \
              -L IMAPtoMbox.log
      The -i argument supplies the imap server name / the username / user's password.    
      See the usage() notes for additional arguments, such as -M to copy only certain IMAP 
      mailboxes, -d to turn on debugging output and -o user to set the ownership of 
      the mailbox files. When used with -M the -r argument will copy all submailboxes
      of the ones in the mailbox list.

    can be used to copy the messages in an IMAP account to
      a user's Maildir.
      Usage: -S IMAPHost/User/Password -u <user> -M <maildir>
      For example:  ./ -S localhost/rich/pwd -u rick -M /users/rick/Maildir
      Optional arguments:
           -d debug
           -I log IMAP commands
           -L logfile
           -m mailbox list (eg "Inbox,Drafts,Notes". Default is all mailboxes)
           -a <DD-MMM-YYYY> copy only messages after this date

    can be used to copy the messages in a maildir to IMAP               
      Usage: -i <users list> -D <imapHost[:port]> 
      Each line in the user list must contain a pointer to the user's maildir, 
      his IMAP username, and his IMAP password with the values separated by 
      commas.  For example:
      #  Format:  maildir,IMAP user,IMAP password
      Optional arguments:
      -L logfile
      -n <integer>  (The number of child processes to run, default is 1)
      -d debug
      -I log IMAP protocol messages

    is used to copy the contents of Unix mailfiles to IMAP               
      mailboxes.  It parses the mailfiles into separate messages which are 
      inserted into the corresponding IMAP mailbox.           
      Usage: -m mailfiles -i server/user/pwd
      Optional arguments:
      -f <mailfile> full filespec of individual mailfile
      -n <name of IMAP mailbox> Used with -f argument
      -L logfile
      -d debug
      -r <range of messages to copy>  for example, 1-500 or 25-75
      -R remove messages from mailfile after copying to the IMAP server

      mbxIMAPsync is used to synchronize the contents of a Unix      
      mailfile with an IMAP mailbox.  The user supplies the location   
      & name of the Unix mailbox (eg /var/mail/rfs) and the hostname,   
      username, & password of the IMAP account along with the name      
      of the IMAP mailbox.  For example:                                
      Usage: -f /var/mail/rfs -i imapsrv/rfs/mypass -m INBOX  
      mbxIMAPsync compares the messages in the mailfile with those in   
      the IMAP mailbox by Message-Id and adds the ones in the mailfile  
      which are not in the IMAP mailbox.  Then it looks for messages    
      in the IMAP mailbox which are not in the mailfile and removes     
      them from the IMAP mailbox.      

      This script deletes all of the messages in a user's IMAP          
                                                                      is called like this:                                  
          ./ -s host/user/password -m mailbox               
      Note that the mailbox name is case-sensitive.                     
      Optional arguments:                                               
          -d debug                                                      
          -L logfile       

    14. opens a connection to an IMAP server, logs in with 
      a username and password, searches mailboxes for messages marked 
      for deletion, and moves them to the trash folder.  The name of
      the trash folder is supplied with the -t argument.
      For example:
      Usage: -S server/user/pwd -t "Wastebasket"
      Optional arguments:
      -m mailbox_list (eg "Inbox, Drafts, Notes". Default is all mailboxes)
      -e  empty the trash can
      -L  logfile
      -d debug
      For example:
          Checking mailboxes for deleted messages...
             Moved 2 messages from Drafts to Wastebasket
             Moved 4 messages from Sent Items to Wastebasket
             Moved 45 messages from INBOX to Wastebasket
          51 messages were moved to Wastebasket
          The Wastebasket mailbox has been emptied

      mozillatoIMAP is a tool for copying messages from a Mozilla mailfile 
      to an IMAP server.  You supply the script with the name of the IMAP
      server along with your username and password.  The -m argument is
      used to indicate the root directory where the Mozilla mailfiles
      reside.  For example:
      "/DOCUMENTS AND SETTINGS/RICK/APPLICATION DATA/Mozilla/Profiles/default/dlss3fw9.slt/Mail/"
      Usage: -i host/username/password -m <mozilla root>
      Optional arguments:
      -L logfile
      -d debug mode 
      -I show IMAP protocol exchanges
      Note that mozillatoIMAP does not map Mozilla message status codes to
      IMAP flags. There is a comment in the code pointing to a URL with a
      table of Mozilla codes if someone wants to implement them.

      delIMAPdups looks for duplicate IMAP messages in a mailbox by    
      finding messages in each mailbox that have the same Message   
      ID.  When a duplicate message is found the DELETED flag is set.  
      If the -p argument has been supplied then an EXPUNGE operation   
      is executed on the mailbox in which the message resides to remove
      Note that delIMAPdups does not check for duplicate copies of     
      messages across multiple mailboxes since it is often useful to   
      cross-file messages in multiple mailboxes.                       
      Usage:  delIMAPdups -S host/user/password                         
      Optional Arguments:
      -m <mailbox list>                       
      -p delete the duplicates
      -M <mailbox>   Move duplicate msgs to this mailbox instead of deleting them.
      -u include the date in determining if a message is unique, not just the message-id
      -L <logfile>                
      -d debug
      -H Use an MD5-digest hash of the message body instead of the Message-Id
      -F <header field> Instead of the Message-Id use a different header field to 
      determine uniqueness, eg Subject or From.

      migrateIMAP is a utility for mass migration of messages for a 
      set of users from one IMAP server to another.
      Usage: migrateIMAP -S host1 -D host2 -i <users file>
      Optional arguments:                                               
      -n maxChildren
      -d debug                                                      
      -I show IMAP protocol exchanges
      -a <DD-MMM-YYYY> Migrate only those messages after the specifed date.
      -b <DD-MMM-YYYY> Migrate only those messages before the specifed date.
      -x <delimiter [prefix]> Specify the src mbx delimiter and prefix (if server does not support NAMESPACE command)
      -y <delimiter [prefix]> Specify the dst mbx delimiter and prefix (if server does not support NAMESPACE command)
      -M <file of mailbox name mappings>.  See description of for details.
      -L logfile                                                    
      -U (update mode)
      The user list file (-i <file>) should contain entries like this:              
      The maxChildren argument specifies how many child processes will be executed
      in parallel (the default is 2).  The optimum number of children depends on
      the system's resources but usually 4 or 5 maximizes the performance.  Beyond
      those numbers there is too much contention for resources to obtain additional
      Note:  migrateIMAP is intended for use on Linux/Unix servers since it takes
      advantage of the fork() system call to run multiple, simultaneous child processes
      for higher performance.  fork is not available on Windows so if you are running
      on a Windows platform you should use instead.

      migrateIMAP-win is basically the same as except it does not
      support multiple child processes via fork().
      Usage: migrateIMAP-win -S host1 -D host2 -i <users file>
      Optional arguments:                                               
      -d debug                                                      
      -I show IMAP protocol exchanges
      -L logfile                                                    

      delete_imap_mailboxes can be used to delete one or more mailboxes
      which are identified using a regex.  For example:
      -m "books" will delete book, old_books, books2010
      -m "^books" will delete all mailboxes starting with "books"
      -m "2010$" will delete all mailboxes ending in 2010
      You can combine the criteria into a single filter:
      -m "books|2010$|Old folders|^My personal folder$"
      Usage: -S host/user/password -m <regex>
      Optional arguments:
      -t test mode (show what would have been deleted but don't delete it)
      -d debug
      -I show IMAP protocol exchanges
      -L <logfile>
  • Top
    imapCapability queries an IMAP server and reports which IMAP services
    it supports.
    Usage: -h <host> -u <username> -p <password>
    Optional arguments:                                               
    -d debug                                                      
    -m list the user's folders
    # ./ -h localhost:1430 -u rfs9999 -p mysecret
    The server supports the following IMAP capabilities:
    Please refer to the IMAP RFCs for an explanation of these capabilities.

    Note: SSL Support

    Most of these utilities support SSL. SSL support is implemented using openSSL and the IO::Socket::SSL Perl module. Both must be installed on the system where you are running these tools.

    If you specify port 993 (995 for POP3) then an SSL connection is initiated. If the port number is 143 (110 for POP3) then it will try a non-SSL connection. With any other value the port will be tested to see if it supports SSL. If so, SSL will be used to make the connection; otherwise a non-SSL connection will be made.


    IMAP Tracing and Debugging

    The -d argument will log additional information about what the tool is doing which may be helpful in diagnosing problems. Most of these scripts accept the -I argument which causes logging of the IMAP protocol exchanges between the script and the IMAP server. This can be useful when troubleshooting problems but you probably don't want to use it during normal operations because of the amount of data that gets written to the logfile.

    For example:

    11-08-2007.03:28:18 2114 >> 1 EXAMINE "Inbox"
    11-08-2007.03:28:18 2114 << * FLAGS (\Answered \Flagged \Deleted \Seen \Draft $MDNSent $
    Hidden $Media $Forwarded Junk $Label1 $Label2 $Label3)
    11-08-2007.03:28:18 2114 << * OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Dra
    ft $MDNSent $Hidden $Media $Forwarded Junk $Label1 $Label2 $Label3)] limited
    11-08-2007.03:28:18 2114 << * 86 EXISTS
    11-08-2007.03:28:18 2114 << * 0 RECENT
    11-08-2007.03:28:18 2114 << * OK [UIDNEXT 360] predicted next UID
    11-08-2007.03:28:18 2114 << * OK [UIDVALIDITY 298417783] UIDs valid
    11-08-2007.03:28:18 2114 << 1 OK [READ-ONLY] EXAMINE completed
    Top will display a list of the user's folders.
    Usage: -S host/user/mypassword [-O output file]

    Administrator Login

    Some IMAP servers allow you to use an adminstrator password to log into
    a user's account.  Although this feature (sometimes called Triplet 
    authentication) is not supported by most servers it is a 
    is very convenient for doing mass migrations on servers that do support it.
    The syntax usually is "domain/adminuser/mailbox adminpassword".