4

Here is an example of how I have configured sieve to forward any mail sent to [nameA|nameB|nameC]@example.org to my private email address.

if address :localpart :is ["To","Cc","Bcc"]
 ["nameA", "nameB", "nameC"] {
    redirect "<my private email address>";
    stop;
}

Sometimes though, email is not forwarded because the address that it was sent to is tucked away somewhere in a "Received" header.

Received: from ###server### ([###ip_address###])
    by ###server### with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256)
    (Exim 4.84)
    (envelope-from <###email_address###>)
    id 1alDM0-0000yT-60
    for nameA@example.org; Wed, 30 Mar 2016 12:28:00 +0200

Is there an effective way to catch these emails in the sieve rule, too?

Paul
  • 766
  • 9
  • 28
  • You seem to be trying to handle Bcc: by parsing headers. There is no guarantee that the `Received:` headers will contain the destination address, either; the only place this information is unambiguously exposed is in the SMTP envelope, which is typically discarded. If you can configure your mail server to always copy the SMTP envelope recipient to a particular header, you won't need this; but then, perhaps you would not need Sieve at all. – tripleee Mar 30 '16 at 11:40

2 Answers2

4

You have an XY Problem here. What you actually want to do here is filter based on the address being delivered to, not the address in the headers. (As unintuitive as it may be, the address in the headers may have nothing to do with the address it's being delivered to, which is how Bcc can work at all.)

The command to test against the actual SMTP envelope is envelope.

require "envelope";
if envelope :localpart :is "to" ["nameA", "nameB", "nameC"] {
    redirect "<my private email address>";
    stop;
}

This will handle all mail being delivered to those names, regardless of whether they show up in the mail headers or not.

1

With the help of the sieves' index feature you can parse the recipient address out of the Received headers.

For BCC sorting I typically do something like this:

require ["fileinto", "envelope", "variables", "mailbox", "index", "subaddress"];
...
if header :index 3 :matches "Received" "*<*@example.com>*" {   
  set :lower "foldername" "${2}";
  fileinto :create "inbox.${foldername}";
} elsif header :index 2 :matches "Received" "*<*@example.com>*" {       
  set :lower "foldername" "${2}"; 
  fileinto :create "inbox.${foldername}";
}
...

In the Received headers of the mails I receive the adress is set in angle brackets and that's why I've chosen the pattern in the example above.

Additionally, sometimes the number of Received headers varies thus I test at least for two different ones.

Zilon
  • 11
  • 1