procmail
[Top] [All Lists]

Re: extracting the TO username

2000-05-16 03:36:15
On May 15,  3:49am, Rav Ahuja wrote:
Subject: extracting the TO username

I am trying to write a recipe that gets the USERNAME from the "To" field (or
Received for) of the incoming message, passes it to a program which accesses
a database and returns an address to procmail to forward the email.

That is, if the incoming email is meant for abc(_at_)mydomain(_dot_)com I 
want the
recipe to be able to extract "abc" (what I referred to as USERNAME ealier)
and pass it as a parameter to the program. My program then returns
xyz(_at_)somedomain(_dot_)com(_dot_)

So something like this should do the trick:

:0
! `myscript "$USERNAME"`

But I can't figure out how to extract the $USERNAME before passing it to the
database program. Any hints on how to do this would be greatly appreciated.


Perl has a module for parsing e-mail addresses.  It's called
MailTools, available at:
http://www.perl.com/CPAN-local//modules/by-module/Mail/MailTools-1.1401.tar.gz


Here's a simple example program, the Address tool:

#!/usr/local/bin/perl -w
use strict;
use Mail::Address;
my ($full_address) = join (" ", @ARGV);
print "full address: ",  $full_address, "\n";
my @addrs = Mail::Address->parse($full_address);
foreach my $addr (@addrs)
  {
    print "address: ", $addr->address, " user: ", $addr->user, "\n";
  }

And an a one line invocation:

% perl mailaddr.pl Joe User '<joe(_at_)sixpack(_dot_)com>'
full address: Joe User <joe(_at_)sixpack(_dot_)com>
address: joe(_at_)sixpack(_dot_)com user: joe

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