bashcharacter-encodingmaildir

read mail out of maildir direcory with correct encoding and use in bash


I'm trying to implement some sort of spamreport for my Mail Server. Mails are sorted by sieve and are all in one folder called Spam. I loop through the folder with bash. With this i get the necessary informations from the mail:

cat $f | grep '^From' | head -n1 >> $TMPFILE
cat $f | grep '^Subject' | head -n1 >> $TMPFILE

but in some mails the subject is encoded like this

Subject: =?ISO-8859-1?Q?Test:_Jaguar_XKR-S:_Unter_dem_Blech_lauert_d?=

How i can get the subject in correct encoding? I tried to use mail, mailx, mutt... but no one was able to simply load a mail from file.


Solution

  • The encoding in the Subject line looks like MIME Words. One possible way to decode the data is write a perl script that uses the MIME::Words module. You could make the perl script a shell script and call it from your bash script.

    convert_subject.sh:

     #!/bin/sh
     /usr/bin/perl -pe 'use MIME::Words(decode_mimewords); $_=decode_mimewords($_);'
    

    Example of using the script:

    $ echo "=?ISO-8859-1?Q?Test:_Jaguar_XKR-S:_Unter_dem_Blech_lauert_d?=" | sh convert_subject.sh
    

    Which outputs:

    Test: Jaguar XKR-S: Unter dem Blech lauert d