Exim Filter Specification section 29

Previous Next Contents (Exim Filter Specification)

## 29. Testing a list of addresses

There is a facility for looping through a list of addresses and applying a condition to each of them. It takes the form

`` `foranyaddress <string> (<condition>)`

where <string> is interpreted as a list of RFC 2822 addresses, as in a typical header line, and <condition> is any valid filter condition or combination of conditions. The ``group'' syntax that is defined for certain header lines that contain addresses is supported.

The parentheses surrounding the condition are mandatory, to delimit it from possible further sub-conditions of the enclosing `if` command. Within the condition, the expansion variable `$thisaddress` is set to the non-comment portion of each of the addresses in the string in turn. For example, if the string is

      B.Simpson <bart@sfld.example>, lisa@sfld.example (his sister)

then `$thisaddress` would take on the values `bart@sfld.example` and `lisa@sfld.example` in turn.

If there are no valid addresses in the list, the whole condition is false. If the internal condition is true for any one address, the overall condition is true and the loop ends. If the internal condition is false for all addresses in the list, the overall condition is false. This example tests for the presence of an eight-digit local part in any address in a `To:` header:

      if foranyaddress $h_to: ( $thisaddress matches ^\\d{8}@ ) then ...

When the overall condition is true, the value of `$thisaddress` in the commands that follow `then` is the last value it took on inside the loop. At the end of the `if` command, the value of `$thisaddress` is reset to what it was before. It is best to avoid the use of multiple occurrences of `foranyaddress`, nested or otherwise, in a single `if` command, if the value of `$thisaddress` is to be used afterwards, because it isn't always clear what the value will be. Nested `if` commands should be used instead.

Header lines can be joined together if a check is to be applied to more than one of them. For example:

      if foranyaddress $h_to:,$h_cc: ....

scans through the addresses in both the `To:` and the `Cc:` headers.

Previous Next Contents (Exim Filter Specification)