简体   繁体   中英

How to decode a base64-encoded email subject string using a Procmail recipe?

This is a simple Procmail recipe to filter emails depending on subject:

:0
* ^Subject: .*Content to filter
$HOME/Maildir/.INBOX.My_filtered_folder/

The above very simple Procmail recipe works without problems. But some emails have Base64 encoded Subject content like this example:

Subject: =?UTF-8?B?VmlldGFsaXMgUG9sZXBl?=
 =?UTF-8?B?bGwaY29tbWVudPVkIG9u?=
 =?UTF-8?B?IGAgbGlubyBCYWPrcGFj?=
 =?UTF-8?B?a2Vw4oDZcyBGZWxpY3l0eSA=?=
 =?UTF-8?B?c3hjckVkLg==?=

So it's needed to decode this encoded Subject content first to be able to filter the emails like in the above recipe.

How is it possible to decode such base64-encoded email subject strings within a Procmail recipe prior to filter for Subject content (on Debian 7)?

This question is NOT a duplicate to a question about "Sendmail/procmail - get mail sender and mail subject, utf8 encoding issues [closed]" – it's quite a completely different question.

There's often a "base64" program installed on Linux and MacOS machines. To do the same thing in Perl specifically, a command line invocation like this would work:

$ perl -MMIME::Base64 -e  'print decode_base64("QWxhZGRpbjpvcGVuIHNlc2FtZQ==");'

Where QWxhZGRpbjpvcGVuIHNlc2FtZQ== is your base64 string.

In my .procmailrc, I have done things like this:

SUBJECT=`formail -x Subject:`

:0
{
# You would use 'fbw' to scan the body
:0 fw 
# presumably any base64 message will have this content header
* ^Content-Transfer-Encoding: base64
| some-external-app
}

where "some-external-app" could be the base64 program, or a suitable perl invocation:

formail -i "Subject: `perl -MMIME::Base64 -e 'print decode_base64($SUBJECT)' `"

or whatnot.

On my machine (netBSD 7.1 on AMD) base64 works like like so:

base64 -d

but YMMV from platform to platform.

Also, the reality is that emails are often malformed in the wild and you may have to tweak the above recipe to match reality. But hopefully you get the idea.

It's a good idea to set the LOGFILE variable in your .procmailrc so you can keep track of unintended consequences. Better yet, use something besides procmail ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM