How to convert mbox files to dovecot folders

Assume you have a bunch of files, each containing a number of email messages stored in mbox format. You want them available through Dovecot, with each mbox file corresponding to a separate subfolder of the same name.

For the sake of this documentation, I'll assume you have dovecot configured with mail_location=maildir:~/.maildir. I'll also assume that you want the a new subfolder for every input file, and that all these folders should be inside the subtree Parent/folder.

Then the following command should do the trick, if executed in the directory where the mbox files are stored.

for i in *; do
    mkdir ~/.maildir/.Parent.folder.$i/{,cur,new,tmp}
    formail -s /usr/libexec/dovecot/dovecot-lda -m Parent.folder.$i < $i
done

Note that these instructions assume Dovecot version 2 as well as the formail command as provided by procmail.