procmail
[Top] [All Lists]

Re: Reading a list of email addresses from a flat file and piping the same to a procmail recipe

2010-09-24 03:55:52
At 20:58 2010-09-21, Komal Tagdiwala (ktagdiwa) wrote:
I have a requirement where I need to read a huge list of email addresses (a kind of white list) from a FLAT file and PIPE the results to my procmail recipe's filter condition for Cc field. If one or more addresses matches with the value in the Cc field of the incoming email, I need to append that matched address to the To: field.

Grep can do this for you, with a bit of help to massage the results. I'd have posted this sooner, but you know, I didn't have this just kicking around, and hoped someone else might post. I've been trying to avoid doing other people's work, but since you posted a recipe showing that you'd _tried_ to accomplish the task yourself, I figured you at least werent' one of the many that just post asking someone else to write everything for them because they're unwilling to try.

Good luck with it, and hopefully you're getting this in time for whatever implementation you need.

# this pipes your message to formail to extract the Cc: header, then
# converts comma delimiters to newlines (so each apparent Cc: recipient
# is a separate line for matching), greps against a wordlist file (your
# address list), translates newlines to commas, removes leading whitespace,
# and trailing whitespace/comma (for last in list).  The results are
# captured into $CCMATCH.
CCMATCH=`(formail -xCc: | tr ',' '\n' | grep -i -w -f addressfile) | tr '\n' ',' | sed -e 's/^[ ]*//' -e 's/[ ]*,$//'`

# if the string isn't empty, there was a match - do something
:0
* ! CCMATCH ?? ^^^^
{
        # concatenate the original To: (extracted in my sandbox framework)
        # after the matched Cc: entries
        :0
        * TO ?? ^^^^
        {
                # To: is empty, so new to is just the match from Cc:
                TOREPLACE=$CCMATCH
        }

        :0E
        {
                # To: is not empty, concatenate match from Cc:
                TOREPLACE="${TO}, ${CCMATCH}"
        }

        # you just want to modify the header, so flag accordingly.
        :0hf
        | formail -I"To: ${TOREPLACE}"
}

# at this point, either you have the original message that didn't have any
# matching entries in the CC, or the message has been modified to put the
# matching addresses into the To:


caveats:
* the insertion recipe relies upon a $TO variable which is captured by my sandbox config (simple operation - see the link in my .sig). I point you to my sandbox rather than cite the simple extraction, because I think you'll find other benefit from reviewing the sandbox and its purpose.

        * no effort is made to remove the addresses from the Cc: header

        * commas within the address comment portions could be problematic

---
 Sean B. Straw / Professional Software Engineering

 Procmail disclaimer: <http://www.professional.org/procmail/disclaimer.html>
 Please DO NOT carbon me on list replies.  I'll get my copy from the list.

____________________________________________________________
procmail mailing list   Procmail homepage: http://www.procmail.org/
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)de
http://mailman.rwth-aachen.de/mailman/listinfo/procmail

<Prev in Thread] Current Thread [Next in Thread>