1

i have another UTF-8 problem. I use munpack -C $extrdir $file to automatically extract JPG files from emails that i received with fetchmail. Afterwards I use bash and imagemagick to process images that i can get with find -iname "*.jpg*" -o -iname "*.jpeg*" -o -iname "*.JPEG*" -o -iname "*.JPG*". This works fine for most images, as long as no spaces or special characters are added to the filename.

In the email sourcecode the filename is written as

Content-Disposition: attachment; filename="=?UTF-8?B?V2FuZGVydW5nIFNwaWVsd2VnIDIuSlBH?=" 

. munpack save the file as

=XUTF-8XBXV2FuZGVydW5nIFNwaWVsd2VnIDIuSlBHX=

I could add "*XUTF*" to find, but then other types of attachements like txt-files might be processed by imagemagick as well. munpack also replaces "?" with "X" that is why I can not decode the filenames.

Do you have a solution for this problem?

Thanks in advance!

Markus
  • 269

1 Answers1

0

This workaround has done it for me. Unfortunately, the filenames are lost. But that was not what i was after.

Use munpack and pipe the output in a file:

munpack -C $extrdir $file > attachments

The content of the attachements file you will now see what was extracted:

=XUTF-8XBXV2FuZGVydW5nTGFpdHNjaGVuYmFjaDExLkpQRw==X= (image/jpeg)
=XUTF-8XBXV2FuZGVydW5nTGFpdHNjaGVuYmFjaDE3LkpQRw==X= (image/jpeg)
...

Now loop through each of the lines, check if it is a images and rename them:

while read p; do
    if [[ $p == *"image"* ]]
    then
        FLNM=attach_${num}.jpg
        IFS=' (' read -a array <<< "$p"
        mv "${array[0]}" "$FLNM"
    fi
    num=$((num + 1))
done <attachements

The output in the directory will then be:

attach_1.jpg
attach_2.jpg
attach_3.jpg
Markus
  • 269