While Mail Forwarding with exim, how to rewrite the To header with true destination address

exim

I have mail forwarding setup with exim using a domain forwarding file.

virtual_aliases_nostar:
  driver = redirect
  allow_defer
  allow_fail
  data = ${if exists{/etc/valiases/$domain}{${lookup{$local_part@$domain}lsearch{/etc/valiases/$domain}}}}
  file_transport = address_file
  group = mail
  pipe_transport = virtual_address_pipe
  retry_use_local_part
  domains = lsearch;/etc/localdomains
  unseen

It is working fine.

However, I would like to rewrite the "to" header. In my system filter, I would like to put something like:

  headers remove to
  headers add "To: $recipient:"

I've tried:

  headers remove to
  headers add "To: $recipient:"

  headers remove to
  headers add "To: $h_env-to:"

  headers remove to
  headers add "To: $env-to:"

The intent is to have the end recipient see their own email address in the To: line of their mail client.

I can't seem to figure out what the correct header is for the final destination of the email so that I can put it in the to header. I've read through the Exim docs and can't seem to find it. I've also looked in the headers in an email at a mail client and can't see it there either.

Any suggestions would be appreciated.

Best Answer

You want to replace the existing To: header (which may contain other recipients or the name of a mailing list) with the computed final destination of the email? You already have some code to figure out where the email has to end up, so use that:

headers remove to
headers add "To: ${if exists{/etc/valiases/$domain}{${lookup{$local_part@$domain}lsearch{/etc/valiases/$domain}}}}"