Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decode the utf8 to ISO-8859-1 mail subject to text in .procmailrc file

Set out to write a simple procmail recipie that would forward the mail if it found the text "ABC Store: New Order" in the subject.

 :0
    * ^(To|From).*[email protected]
    * ^Subject:.*ABC Store: New Order*
    {

Unfortunately the subject field in the mail message coming from the mail server was in MIME encoded-word syntax.

Subject: =?UTF-8?B?QUJDIFN0b3JlOiBOZXcgT3JkZXI=?=

The above subject is utf-8 ISO-8859-1 charset, So was wondering if there are any mechanisms/scripts/utilities to parse this and convert to string format so that I could apply my procmail filter.

like image 560
MON Avatar asked Apr 18 '15 08:04

MON


2 Answers

You may use perl one liner to decode Subject: before assigment to procmail variable.

# Store "may be encoded" Subject: into $SUBJECT after conversion to ISO-8859-1
:0 h
* ^Subject:.*=\?
SUBJECT=| formail -cXSubject: | perl -MEncode=from_to -pe 'from_to $_, "MIME-Header", "iso-8859-1"'

# Store all remaining cases of Subject: into $SUBJECT
:0 hE
SUBJECT=| formail -cXSubject:

# trigger recipe based also on $SUBJECT content
:0
* ^(To|From).*[email protected]
* SUBJECT ?? ^Subject:.*ABC Store: New Order
{
....
}

Comment (2020-03-07): It may be better to convert to UTF-8 charset instead of ISO-8859-*.

like image 78
AnFi Avatar answered Sep 22 '22 05:09

AnFi


You should use MIME::EncWords.

Like this

use strict;
use warnings;
use 5.010;

use MIME::EncWords 'decode_mimewords';

my $subject = '=?UTF-8?B?QUJDIFN0b3JlOiBOZXcgT3JkZXI=?=';
my $decoded = decode_mimewords($subject);
say $decoded;

output

ABC Store: New Order
like image 31
Borodin Avatar answered Sep 20 '22 05:09

Borodin