2

I answered this question with my current solution, but as I said I'm not 100% happy with it. That solution to the question 'How to add a custom header to all outgoing mail' involved adding the headers_add parameter into the existing Smarthost transport. However, I can see updates or reconfiguration reverting my edit at some point without me knowing.

I had hoped to create a minimal router which would run early on in the chain and just add the required header - this header can be added to all mail, whether it ends up being for local or remote delivery. But I couldn't find any example of a router which runs, adds the header then passes all messages on to the next router in the chain. I created this, which seems to work, but I'm very new to Exim and would be grateful if anyone can point out flaws or a better way to achieve the desired outcome. The router is /etc/exim4/conf.d/router/01_exim4-config_dev_header:

dev_header:
    debug_print = "R: dev_header for $local_part@$domain"
    driver = redirect
    headers_add = "X-Test-Dev-Header: true"
    data = $local_part@$domain
    self = pass
dsl101
  • 433
  • 1
  • 7
  • 13

1 Answers1

0

You don't need a router to do this. It is much more easier (and, in my opinion, cleaner) to have a statement in the acl_smtp_data ACL, which does nothing but adds the header, like this:

warn add_header = X-Test-Dev-Header: true

See "Adding header lines in ACLs" for more info.

How to enable this rule

I'm assuming you have the "split" configuration.

  1. Look for a file named main/0000-localmacros or similar. If it doesn't exist, create it.
  2. The file should contain a line like this:

    CHECK_DATA_LOCAL_ACL_FILE=/etc/exim4/some/file
    

    If it doesn't, add this line. The filename can be anything.

  3. Write the line above into the /etc/exim4/some/file file (the warn add_header... one).
  4. Reconfigure exim. On Debian this is done by running the dpkg-reconfigure exim4-config command.

After this, every mail should contain the header you specified.

Lacek
  • 6,585
  • 22
  • 28
  • I read that documentation as only being able to add headers to received mail, not sent mail - "The add_header modifier is permitted in the MAIL, RCPT, PREDATA, DATA, MIME, DKIM, and non-SMTP ACLs (in other words, those that are concerned with receiving a message)." Or am I misunderstanding what 'receiving' means here? – dsl101 May 27 '16 at 13:06
  • I just create a file called `10_exim4-config_devmail` with this content: `acl_smtp_data: warn add_header = X-Test-Dev-Header: true` and while exim didn't reject the new configuration, the header doesn't appear in outgoing mail... – dsl101 May 27 '16 at 13:13
  • Yes, you misunderstood. From exim's viewpoint "receiving" a message is the process of getting a message through the network. It is irrelevant what will happen with the message. If you use exim as a smarthost, it will "receive" your message, and "send" it afterwards if the address isn't a local one. The documentation says you can add headers while receiving a message, but not when you're about to send it out. – Lacek May 27 '16 at 13:25
  • I've edited my answer to contain instructions on how to enable the rule in exim. – Lacek May 27 '16 at 13:32
  • Thanks for the detailed instructions. I am trying to add this header to mail generated locally for remove delivery via a smarthost, and I'm pretty sure I followed - I have split config, and there was no `0000-localmacros` in `/var/exim4/conf.d/main` so I created it with contents `CHECK_DATA_LOCAL_ACL_FILE=/etc/exim4/dev.headers` and `/etc/exim4/dev.headers` contains `warn add_header = X-Test-Dev-Header: true`. I then ran `dpkg-reconfigure exim4-config`, accepting all existing values. Exim restarted, no console warnings, but still no headers. – dsl101 May 27 '16 at 14:03
  • And in case it helps, here are the 2 lines from the exim mainlog when sending a test mail from root to an external address (some bits obfuscated): `2016-05-27 09:56:51 1b6IFv-0006b4-0N <= root@xxx.yyy.zzz U=root P=local S=485` `2016-05-27 09:56:51 1b6IFv-0006b4-0N => xxx.yyy@gmail.com R=smarthost T=remote_smtp_smarthost H=smtp.mailgun.org [104.130.177.23] X=TLS1.2:RSA_AES_128_CBC_SHA1:128 DN="C=US,ST=Texas,L=San Antonio,O=Rackspace US\, Inc.,CN=*.mailgun.org"` Is that where the `warn` output should appear if it was working? – dsl101 May 27 '16 at 14:04
  • Sending "local" mails (i.e. from the command-line) is handled in the `acl_not_smtp*` ACLs. These mails require special handling, so most of the ACLs simply doesn't get called for them. If you send a mail from the network, it should work. The split config file (to my knowledge) doesn't contain any macros of not_smtp ACLs, so adding them to such config is not straightforward. If you need help with that, I think it's best to ask it in an other post. – Lacek May 27 '16 at 14:26
  • All mail I'm trying to affect is generated locally on that server, not received over the network - notifications, etc. from scripts as part of a big CMS. So I guess this ACL approach won't work sadly. – dsl101 May 27 '16 at 15:21