procmail
[Top] [All Lists]

Re: How to extract multiple email addresses in To: header

2005-05-13 14:10:57
On Fri, May 13, 2005 at 10:47:08AM -0500, mark david mcCreary wrote:

Can someone point me to a recipe that will extract pure email
addresses (not the text verbage) from the To and Cc headers.

For example,

To: Joe Schmoo <joe(_at_)schmoo(_dot_)com>, Bob Smith 
<bob(_at_)smith(_dot_)org>
Cc: Sam Spade <sam(_at_)spade(_dot_)com>

I want to end up with a procmail variable containing

joe(_at_)schmoo(_dot_)com, bob(_at_)smith(_dot_)org, sam(_at_)spade(_dot_)com

I see where the -rt of formail will flip the Sender address stripping
verbage.  However, I want the To line, not the From line.

We've discussed it on this list a few times.  (Check the archives
as a first step.)

It is possible, but complex and a bit messy, to do it in pure procmail.
However, I can tell you that as soon as you have an algorithm in procmail
to perform a function on an unknown number of args, you are going to
be doing it with recursive calls to an INCLUDERC or SHIFTRC.  Again,
doable, but a rather tricky undertaking.

You can of course use a sed expression or script, other other
shell expression or script, as someone else already suggested.
Remember that there are various RFC-compliant ways to express
"friendly-name" comments in email addresses, and you'd want to
test for them all.

For a quick, not-too-"expensive" solution, I'd use the capability
pre-built into formail.  You stated that formail can do this with
the sender address.  Well, one can trick formail by swapping the
To address for the Sender address, then doing that.  For
exmaple, this seems to work:

 formail -I "From" -I "Return-Path:" -R "To:" "From:" -rtzx "From"

However, this won't do what you want: it takes only the first address,
not all the addresses.  So you won't have a comma-generated list.
Example is a message to me and this list:

 10:49pm [~/Mail/.mailcache] 615[0]> formail -XTo < msg.g-0D                    
                                   
To: Dallman Ross <dman(_at_)nomotek(_dot_)com>, 
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)DE

 10:49pm [~/Mail/.mailcache] 616[0]> formail -I "From" -I "Return-Path:" -R 
"To:" "From:" -rtzx "From" < msg.g-0D
dman(_at_)nomotek(_dot_)com


So, again, that won't do what you want.

-- 
dman

____________________________________________________________
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