Franklin

Plus address forwarding in Postfix

We all know Gmail or Outlook and they have this great feature where you can use multiple aliasses for only one real emailaddress like [email protected]. It is called address tagging, plus-addressing, plus-forwarding, sub-addressing or even sub-aliassing, and existed way before these services came around. This method allows you to ‘tag’ the sender of the mails and apply custom filtering. Also it allows you to see if they are sharing your emailaddress with other parties.

  1. How it works
  2. — Postfix
  3. — Validating forms
  4. Configuration (tl;dr)
  5. Related stuff

How it works

Normally you have an address like [email protected]. With tagging you can use addresses like [email protected] while your normal address remains the same. For example, mails to [email protected] are delivered to [email protected] and in the inbox filters you can create a rule to move all [email protected] mails to another folder.

For mail services like Gmail and Outlook.com it works out of the box. When you run your own mailserver, especially Postfix, you may first need to enable this feature.

— Postfix

To enable this behaviour in Postfix you only have to set the recipient_delimiter in main.cf and reload the daemon, but there is a catch. For some reason unknown to mankind Postfix is appending the inbound ‘tag’ to the forward email address, so that becomes [email protected] causing problems with delivery. Postfix attempts to tell the mailserver at ‘forward.tld’ in the SMTP RCPT TO command it has mail for the ‘othername+tag’ user, which doesn’t exist since it should be only ‘othername’. You need to rewrite the delivery address using recipient_canonical_maps.

To clarify:

recipient_delimiter applies to incoming mail.

recipient_canonical_maps is essentially the same but for outgoing mail.

See Configuration below for details.

— Validating forms

Okay, so finally you set up plus tagging but then there is yet another problem: Javascript forms. You know the kind that attempts to validate the emailaddress. They quiet often refuse the + sign because it is not a so called word-character in the regular expression matching they perform. Those often only include A to Z, 0 to 9, period, underscore and hyphen.

The last one, hyphen, is not often used so theoretically you could start using that one. I used to recommend that untill I had to mail someone like ‘[email protected]’ causing Postfix to deliver the mail to ‘[email protected]’ which belongs to someone else.

Configuration (tl;dr)

First in /etc/postfix/main.cf add or set:

# Enable address-tagging for the + character
# You can append more if you need to, like: +-
recipient_delimiter = +

# Remove the tag from the forward address
recipient_canonical_maps = regexp:/etc/postfix/recipient_canonical_maps

Now add this to /etc/postfix/recipient_canonical_maps (one line!)

/^(.+)\[email protected](.*)/ ${1}@${2}

When using multiple characters in ‘recipient_delimiter’ above, use this line instead:

/^(.+)[\+-][email protected](.*)/ ${1}@${2}

(note the [\+-])

And reload postfix

sudo service postfix reload

That’s all.

Note that any alias or forward matching the tagged address will override this configuration.

A sidenote on testing: when sending mail to yourself, even via forwarders, Gmail will accept the mail but won’t deliver it to your inbox. That is because it is counted as a duplicate. Better test from another account.

Changelog
2017-05-09 – Clarify the configuration parts
2017-05-05 – Minor tweaks regarding hyphen use.
2017-04-27 – Removed the part about using a hyphen for the tag, it is actually used by some folks as their email address.

1 Comment

  1. “A sidenote on testing: when sending mail to yourself, even via forwarders, Gmail will accept the mail but won’t deliver it to your inbox. That is because it is counted as a duplicate. Better test from another account.”

    I was trying to set +forwarding with postfix and tested with main Gmail account which didn’t work – thanks for this reminder!

Leave a Reply