Welcome!‎ > ‎Science‎ > ‎Computer‎ > ‎


Recently, I was required to download all my eMail from my IMAP-mail account. Doing this manually seemed like a lot of work, so needed to come up with a neat solution.

On GitHub I found Rob's python code "gmail_imap_example.py" which comes along with a nice description on his blog voidnullness. Rob's code was a good starting point for my task. However I did not only want to download my eMails but also extract header information and attachments from the downloaded files. So I needed to tweak his code a bit and came up with "maildump.py":

Gist #4edaa9c703915e72c867

Having downloaded all my eMails from my IMAP account using the python code above, I decided to convert my eMails into PDF documents. For this task I used the python "txt2pdf.py" program together with my short bash script:

for ((i=1;i<=NUMBEROFFILES;i++)); do 
    filename0="$(printf "FOLDERmail-%04d.eml" "$i")"
    filename1="$(printf "FOLDERmail-%04d-head.txt" "$i")"
    filename2="$(printf "FOLDERmail-%04d-part-01.ksh" "$i")"
    filename3="$(printf "FOLDERmail-%04d-part-01.html" "$i")"
    filename4="$(printf "FOLDERmail-%04d-text.txt" "$i")"
    echo $filename0 > $filename4
    echo >> $filename4
    cat $filename1 $filename2 $filename3 >> $filename4
    python txt2pdf.py  $filename4
    rm $filename1
    rm $filename2
    rm $filename3
    rm $filename4

This bash script is a quick and dirty solution. As such, it requires adaptation: NUMBEROFFILES, FOLDER and %04d are hard coded into the script and require manual modification. NUMBEROFFILES represents the total number of eMails in the specified IMAP folder. %04d is responsible for generating file names with leading zeros (this value corresponds to the line DIGITCOUNT=4 in the python script). Finally, FOLDER stands for the directory in which the downloaded eMails can be found.