0

Let's suppose my domain at exampleserver.com. I have following sieve rule:

require ["fileinto", "subaddress", "mailbox", "variables"];

if address :detail :matches "to" "*" {
set "folder" "${1}"; 
fileinto :create "INBOX.${folder}";
}

user@exampleserver.com wants mail forwarded to xxxxx@gmail.com. It is done via /etc/postfix/virtual as

user@exampleserver.com      xxxxx@gmail.com

The forwarding is perfectly good, but mail sent to user+Something@exampleserver.com is now forwarded to user+Something@gmail.com which is utterly wrong. How to fix it so sieved messages get redirected to xxxxx+Something@exampleserver.com?

1 Answers1

1

Postfix' aliasing via virtual is performed before any sieve proceeding. Therefore you have to disable postfix aliasing completely and forward messages by dovecot's sieve only. There is number of modes address is compared against the pattern:

......
# rule:[gmailfwd1]
if header :is "to" "user@exampleserver.com"
{
  redirect "user@gmail.com";
}

# rule:[gmailfwd2]
if header :contains "to" "user"
{
  redirect "otheruser@gmail.com";
}

# rule:[gmailfwd3]
if header :regex "to" "user.*@exampleserver.com"
{
  redirect "otheruser@gmail.com";
}
......

header :is means the strict matching the whole recipient's address

header :contains means that any part of address is matching the string.

header :regex means that recipient's address is matching the regular expression.

Anyway you have to remember that sieve rules are checked from the top to the bottom until some rule will be matched. So you can reorder rules in some sequence where more specific cases will be catched and proceeded before any other unwanted rules.

P.S.

As stated in the dovecot's manual https://wiki.dovecot.org/Pigeonhole/Sieve/Examples#Plus_Addressed_mail_filtering all can be made this way:

require ["variables", "fileinto", "subaddress"];

if header :is :user "to" "someuser" {
  if header :matches :detail "to" "*" {
    set "tag" "${1}";  /* extract "Something" into var "tag" */
  }

  if string :is "${tag}" "" { /* empty/no tag */
    redirect "user@gmail.com";
  } else {                    /* tag is present*/
    redirect "otheruser+${tag}@exampleserver.com";
  }
}
Kondybas
  • 6,964
  • 2
  • 20
  • 24
  • That yields invalid regular expression 'user(+.*)@exampleserver.com' for regex match: invalid preceding regular expression. – Flat Spaztech Jun 14 '18 at 14:32
  • Have you added the plugin? `require ["fileinto","regex"....];` – Kondybas Jun 14 '18 at 14:39
  • Yes:require ["regex", "fileinto", "subaddress", "mailbox", "variables"]; – Flat Spaztech Jun 14 '18 at 14:41
  • I can't test it right now but it look like special meaning of `+` sign. Try to escape it by backslash `(\+.*)` – Kondybas Jun 14 '18 at 14:43
  • Didn't work. I tried to escape the parentheses as well but the result is still the same. If I use user(\\+.*)exampleserver.com (double backslashes) the sievec doesn't give errors. – Flat Spaztech Jun 14 '18 at 14:47
  • I've change my answer – Kondybas Jun 14 '18 at 14:59
  • I've test the first approach on my playground and it works ok. I've used `dovecot-2.3.1` and `pigeonhole-0.5.1` – Kondybas Jun 14 '18 at 15:03
  • Indeed it works - but there's a caveat. Now I must have matching user on my server. Let's say I want to provide an forwarding alias otheruser@exampleserver.com without such user on the server. Postfix will surely report: "Recipient address rejected: User unknown in local recipient table" – Flat Spaztech Jun 14 '18 at 16:00
  • So you should explain your needs in details. Everything is possible with `sieve` – Kondybas Jun 14 '18 at 17:02
  • Well - I need to forward emails to another server (with the +) without adding them as users to my server. – Flat Spaztech Jun 14 '18 at 17:16
  • You can't do it with `sieve` because `sieve` proceed messages at the delivery stage. No user - no delivery - no sieving. I'm not familiar with `postfix` but `exim` is powerful enough to do anything we can imagine. – Kondybas Jun 14 '18 at 17:31