Bash script to convert maildir to mbox format

Bash script to convert maildir to mbox format

Hello Guys,

Maildir and Mbox are email formats which act as a directory for storing messages in email applications. Mbox places all messages in the same file on the server, whereas, Maildir stores messages in individual files with unique names.

In some cases, if user is downloading/exporting emails from webmails like Horde or Roundcube and if the mailfolder having large size, cPanel is unable to handle that many messages and will throw a timeout/memory error or sometimes the file will export with zero byte in size.

We can make use this script to take a backup of mailfolders in mbox format and these mbox file can be imported to Apple Mail, Thunderbird etc from user end.

The conversion is made with the use of 'formail' and please be noted that this script is only applicable to cPanel servers.

Let's get started...

I have take down the script into couple of segments, You can find all the code at my github.com/vyshnavlal/maildir-to-mbox-conve.. GitHub Repository. Drop a star if you find it useful.

#!/bin/bash
#Bash Script to convert Maildir to Mbox using formail in cPanel servers

echo "Enter the Email account: "
read email_account
echo
echo "Enter the mail folder to convert: "
read mail_dir
echo

domain=`echo $email_account | awk -F\@ '{print $2'}`
user_name=`echo $email_account | awk -F\@ '{print $1}'`
user=`fgrep "$domain" /etc/userdomains | awk '{print $2}' | head -1`

Here we are taking inputs from users, ie the email account and the mail-directory he wish to convert. And the required data is storing into variables.

cd /home/$user/mail/$domain/$user_name
#creating a list of custom mail folders
directory_list=( $(ls -a | grep -E "^\.[a-zA-Z0-9]" | awk -F\. '{print $2}') )
#array of possible inputs
possible_inputs=(inbox Inbox INBOX)

By knowing the username entered email account, changing the directory to that and there will be custom mail folders which is created by the user and in maildir format, all the custom folders starts with a dot. Thus we are creating a directory list. Also the most common possible input is taken to an array called possible_inputs

mkdir /home/$user/Mail_backup
if [[ " ${possible_inputs} " =~ " ${mail_dir} " ]]; then
    echo "Converting inbox..."
    echo
    #converting cur to mbox
    for file in `find /home/$user/mail/$domain/$user_name/cur -type f`
    do
        cat $file | formail -A Date: >> /home/$user/Mail_backup/inbox.mbox
    done

    #converting new to mbox
    for file in `find /home/$user/mail/$domain/$user_name/new -type f`
    do
        cat $file | formail -A Date: >> /home/$user/Mail_backup/inbox.mbox
    done
    cd /home/$user/Mail_backup

    echo "Finished converting !"
    echo
elif [[ " ${directory_list[@],,} " =~ " ${mail_dir,,} " ]]; then
    echo "Converting $mail_dir..."
    for file in `find /home/$user/mail/$domain/$user_name/.$mail_dir -type f`
    do
        cat $file | formail -A Date: >> /home/$user/Mail_backup/$mail_dir.mbox
    done
    echo "Finished converting !"
    echo
fi

Created a directory to store the converted mails. The first loop check only for the possible inputs and if it is TRUE then converting the new and cur directory to mbox format and stored in the file inbox.mbox The second loop checks if the maildir entered by the user matches with the directory list, if then converting it to mbox and stored in the file.

#zipping the backup
echo "Creating a zip of the mail backup..."
echo

cd /home/$user
zip -r "$user_name""_""$mail_dir.zip" Mail_backup
rm -rf Mail_backup
chown $user:$user "$user_name""_""$mail_dir.zip"
echo

echo "Zip file created and placed it in /home/"$user" as "$user_name""_""$mail_dir.zip" "

The last segment creating a zip of converted data and changing the permission to user's, thus users can download it to their local system.

Hope this tutorial has helped more for server administrators.

Thank you for reading, Do share your valuable suggestions, I appreciate your honest feedback!