Re: [exim] Extract email address from From header

Top Page
Delete this message
Reply to this message
Author: Phil Pennock
Date:  
To: Elliot Murdock
CC: Exim-users
Subject: Re: [exim] Extract email address from From header
On 2010-10-29 at 03:19 +0200, Elliot Murdock wrote:
> However, the $header_From variable returns not only the email address
> but any names associated with it. For example,
> From: John Doe <johndoe@???> gives John Doe <johndoe@???>
>
> I would need a regex to extract the email address.


That would be a case of having a hammer and thinking you're holding
nails. ;) It's also a world of pain, because while matching the bit of
an email address inside the <...> is doable in a regexp, handling the
whole display format with just a regex is an exercise in masochism. It
can be done, but it's a bit like proving traditional vi is
turing-complete and then using it to solve problems.

The ${addresses:...} expansion operator will help you. It extracts the
addresses from a string, just as you want.

Note that the From: header is defined to be able to take 1 or more
addresses.

So ${addresses:$h_from:} will work, as long as there's just one
address in From. It generates a colon-separated list of the email
addresses, and instances of the list-separator in the data are doubled,
for escaping.

To reliably get just one email address from From:, if you're happy with
it being the last such, then:
${reduce{${addresses:$h_from:}}{}{$item}}

-Phil