1

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.

Monty
  • 11
  • 3
  • 1
    Why did you tag this as Perl? Can you run Perl code in those filters, or is Procmail written in Perl, or is the regex of the PCRE flavor? Only the first option is relevant to the Perl tag. – simbabque Nov 02 '17 at 23:02
  • Possible duplicate of [Sendmail/procmail - get mail sender and mail subject, utf8 encoding issues](https://stackoverflow.com/questions/11726354/sendmail-procmail-get-mail-sender-and-mail-subject-utf8-encoding-issues) – tripleee Nov 03 '17 at 04:49
  • Yes, you can run Perl code in Procmail. Maybe there's a solution that uses a Perl script. – Monty Nov 03 '17 at 20:02
  • It's not a duplicate of Sendmail/procmail - get mail sender and mail subject, utf8 encoding issues. There are some similarities, but this question is not answered. – Monty Nov 03 '17 at 20:09
  • *How* is this different? The accepted answer on that question shows you exactly how to decode your example. – tripleee Jun 01 '18 at 09:53

1 Answers1

2

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 ;-)

G McIntire
  • 41
  • 6
  • Thank you. Could you elaborate it a little bit more? 1. What do you mean with "provided the subject line matches $PATTERN"? 2. "Better yet, use something besides procmail" – Unfortunately I can't change it on this system to e.g. Sieve – but Procmail works really great. – Monty Nov 03 '17 at 20:15
  • An additional complication is that [RFC2047](https://datatracker.ietf.org/doc/rfc2047/) provides for much more than a single base64 token in a single encoding. In today's world, you should probably normalize everything to UTF-8 in a [canonicalized representation](https://en.wikipedia.org/wiki/Unicode_equivalence) (I guess NFD though this also depends on your specific application; NFKC might actually be the simplest and most robust if your needs are pedestrian) – tripleee Jun 01 '18 at 09:50