Re: [procmail] [sed] [awk] cleaning up mail headers

From: Alan Connor (zzzzzz_at_xxx.yyy)
Date: 06/07/04


Date: Mon, 07 Jun 2004 00:00:41 GMT

On Sun, 06 Jun 2004 21:26:21 +1000, Troy Piggins <troy@piggo.com> wrote:
>
>
> Not quite sure where to post this ...
>
> I am trying to write a command line for procmail that takes the From:
> header field from an email and cleans it up into just the email address.
> Reason being that depending on the sender, the From: field could be in
> any of the following formats :
>
> First Last <user@domain.com>
> "First Last" <user@domain.com>
> user@domain.com
> first.last@domain.com.au
>
> and there could be others.
> I want to use just the email address (eg user@domain.com or possibly
> first.last@domain.com.au) to grep my ~/.aliases file to check if I am
> getting mail from someone I trust (whitelist).
> My .aliases file is in mutt's format :
>
> alias nickname1 First1 Last1 <user1@domain1.com>
> alias nickname2 First2 Last2 <user2@domain2.com>
> ...
>
> At present I use the following rule, with list.white being just a
> manually edited list of emails only :
>
>:0:
> * ? formail -x "From:" -x "From" -x "Sender:" \
> | egrep -i -is -f $PROCMAILDIR/list.white
> white
>
> But (I guess) I want something like this :
>
>:0:
> * ? formail -x "From:" -x "From" -x "Sender:" | $CLEAN_FROM_SCRIPT \
> | egrep -i -is -f ~/.aliases
> white
>
> and I envisaged $CLEAN_FROM_SCRIPT being a sed or awk commandline, but
> could be bash script. I need help with that part of it.
> Any suggestions?
> Thanks.

Hi Troy,

The nutters are out in force today, aren't they :-)

(I use passlist/blocklist instead of whitelist/blacklist, because the
latter sound racist to me.)

Couple of thoughts:

An effective passlist contains a lot more than just one's trusted
friends. It should include *anyone* you've sent a mail to, and
you aren't going to create an alias for most of those.

They can also be more complex than simply a return address. Sometimes,
you will not know what address the mail will be returned from. Mail
to any large organization can be like that, where it will be handed
from department to department and may end up being returned from the
home of an employee there...

In that case, it is useful to passlist the Subject: line, and have
something like this at the top of the mail:

 |The Subject of this mail is a password and needs to be included in
 |any reply, unchanged except for Re: (one or more) and whitespace(s).
 |Thank you.

For mailing lists, it is often the Return-Path: you passlist, or a
special header from the listserver.

That being said....

(I'm assuming here that you call fetchmail which then calls
procmail...)

It's a lot easier to parse your_alias_file than the incoming headers,
so do that before fetchmail does its thing, with a script:

#!/bin/bash
# /usr/local/bin/alparse

sed -e 's/^\(.*<\)\(.*\)\(>.*\)/\2|\\/' \
-e '$s/|\\//' -e 's/\./\\./g' \
/home/you/your_alias_file > /home/you/newalias

To make it all automatic, alias (shell alias) fetchmail like so:

alias fetchmail='alparse && fetchmail'

So whenever you call fetchmail, the first thing that happens is
that your_alias_file is converted to something procmail can deal
with, and you can add and subract from it without worrying about
having to do anything else. The old newalias will be overwritten
each time you retrieve your mail.

This goes at the top of your .procmailrc:

ALIAS=`cat /home/you/newalias`

then

:0:
* $ ^(From|From:|Sender:|Reply-To:|Return-Path:).*${ALIAS}
pass

AC

-- 
Pass-List -----> Block-List ----> Challenge-Response
The key to taking control of your mailbox.  Design Parameters:
http://tinyurl.com/2t5kp ||   http://tinyurl.com/3c3ag
Challenge-Response links -- http://tinyurl.com/yrfjb


Relevant Pages

  • Re: Procmail Regex
    ... them to each mailbox using procmail. ... 'Received' header from incoming mails as shown below ... Fetchmail will do this for you without needing recourse to procmail. ...
    (comp.mail.misc)
  • all messages the same date and time (fetchmail)
    ... I use fetchmail to get my e-mail. ... and time (probably because of last header added by postfix). ... Is there a solution (using postfix or procmail) to correct this ...
    (comp.os.linux.networking)
  • Re: [SLE] Fetchmail Daemon Problem
    ... The mail server was rejecting this mails with the header filter, ... > case, to fetchmail, otherwise, to whatever is at the other side. ... Blocking backscatter mail from virus scanners Jun ...
    (SuSE)
  • Re: Fetchmail revisited
    ... > deposited into one email account which I access via pop [my isp calls ... The change is done by both by your provider and fetchmail. ... Use `procmail` to filter the mail. ... forward 'root' to your main account. ...
    (alt.os.linux.suse)
  • Re: [opensuse] Procmail: Filter & Resend
    ... You may better use fetchmail. ... What's missing is the catch-all case (multi drop mailboxes). ... Procmail is a mail delivery agent. ... The problem is that there are a number of Received fields. ...
    (SuSE)