Apple Mail is nice, but it has its own issues. For example in one of the OSX updates, Apple decided to revamp the way Apple Mail and OSX handle email accounts and the update process crashed on me, causing me to spend days trying to recover the data. Thunderbird is a nice and open-source software and some of my emails I could only recover using it: Old Apple Mail mailboxes no longer accessible by the new Apple Mail could be read by Thunderbird.

The main thing that allows Apple Mail data to be imported into Thunderbird is an add-on called ImportExportTools. However while it has an "import directory" option, that option has always failed for me. On the other hand emlx files can easily be imported with this tool. But here is the catch: On occasion (probably for emails with big attachments), Apple Mail takes out the BASE64 encoded attachments out of the emlx file and dumps them into separate files. For example you can see something like:

ls /Users/<username>/Library/Mail/V2/IMAP-<user@mail.xyz>inbox.mbox/*/Data/*/*/Messages/
67147.emlx          67153.2.2.emlxpart  67158.emlx          67164.emlx          67169.emlx
67148.emlx          67153.partial.emlx  67159.emlx          67165.emlx          67170.emlx

So we need to embed the *emlxpart files back into the corresponding *.partial.emlx files before importing into Thunderbird. For this, here is a simple bash+awk script.

#!/bin/bash
# A simple script to merge emlxpart files back into partial.emlx.
# veo, July 10, 2015, released under GPLv3

function merge {
    if [ $# -eq 0 ]; then
        return
    fi
    if [ ! -f $1 ]; then
        echo File $1 could not be found.
        return
    fi
    echo Handling file: $1
    sap=`echo $1 | awk -F. '{print $1}'`
    #echo $sap
    TMPFILE=`mktemp /tmp/mergetmp.XXXXXXXXXX` || exit 1
    ls $sap*emlxpart | awk -F. '{print $2}' > $TMPFILE
    # NR==FNR: read the 1st file to get the list of parts
    # nfile: number of attachment files to be embedded back into the emlx
    # a[]: the indeces of the attachments: nth attachment is the file
    # fileidx: a counter to count the embedded files
    # if fileidx > nfile: we have embedded all the available files
    awk -v sap=$sap 'BEGIN {nattach=0; fileidx=1;}
         {if (NR==FNR) {a[NR]=$1; nfile++; next}
          if ( nattach<a[fileidx] || fileidx>nfile ) print;
          else if ( nattach==a[fileidx] && $0!="" ) print;
          else {
            print; system("cat "sap"."nattach".emlxpart"); print;
            fileidx++;
          }
          if ($1=="Content-Transfer-Encoding:") {
             nattach++;
             }
          }
          END {}' $TMPFILE $1 > $sap-full.emlx
    rm $TMPFILE
}

for partialemlx in `ls *.partial.emlx`
do
    merge $partialemlx
    mv $partialemlx $partialemlx.old
done

-- ErkcanOzcan - 10 Jul 2015
Topic revision: r1 - 10 Jul 2015, ErkcanOzcan
 

This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback