7

I'm creating a bounce email system where addressees can reply to messages on my site.

However when the email are sent to the user containing the previous message, the Reply-To field contains an address like this notification-message-988742@mysite.com (which contains the ID at the end).

If the user replies, the reply message will be sent back to notification-message-988742@mysite.com which of course, doesn't have its own mailbox, except the notification@mysite.com.

How would I redirect all incoming messages coming from a specific wildcard notification-message-*@mysite.com to notification@mysite.com? I did some research, but no solid part worked, including the luser_relay = notification@mysite.com and putting notification-message-* in the postfix aliases table, the notification@ has a Maildir, so the mail would go into it.

A concept diagram:

enter image description here

I am using Ubuntu 11.04.

user2066657
  • 336
  • 2
  • 13
MacMac
  • 1,931
  • 8
  • 30
  • 38
  • Use pcre http://serverfault.com/questions/313050/use-postfix-to-forward-mail-to-a-domain-to-the-same-address-at-a-different-domai –  Oct 09 '12 at 02:41

4 Answers4

13

As have said mschuett, you can use regexp

First check that postfix supports regexp:

root @ mail / #  postconf -m | grep regexp
regexp

Create the file /etc/postfix/aliases-regexp and add to it your regexp

root @ mail / #  cat /etc/postfix/aliases-regexp
/notification-message-[0-9]+@example\.net/ notification@example.net

Run postmap and check whether it works:

root @ mail / #  postmap /etc/postfix/aliases-regexp
root @ mail / #  postmap -q notification-message-123123@example.net regexp:/etc/postfix/aliases-regexp
notification@example.net

If everything is OK, add this file to your alias database

Example:

root @ mail / #  cat /etc/postfix/main.cf | grep ^alias_maps
alias_maps = hash:/etc/aliases regexp:/etc/postfix/aliases-regexp

If you are using virtual domains, add this file to your virtual_alias_maps

Example:

root @ mail / #  cat /etc/postfix/main.cf | grep ^virtual_alias_maps
virtual_alias_maps = mysql:/etc/postfix/mysql/alias.conf regexp:/etc/postfix/aliases-regexp

Do not forget to restart postfix.

Good luck!

cfi
  • 248
  • 5
  • 14
GreyZmeem
  • 366
  • 1
  • 3
  • 1
    Is it possible to store the regex aliases in a database like MySQL or sqlite3, like you do with the `mysql:` lookup table? – gitaarik Dec 17 '14 at 11:14
  • 3
    @rednaw Yes, definitely possible (and preferable IMO)! Just modify the **``query``** used in the ``mysql-virtual-aliases.cf`` file to something like ``query = SELECT destination FROM virtual_aliases WHERE is_regexp=0 AND source='%s' OR is_regexp=1 AND '%s' REGEXP CONCAT('^',source,'$')`` as described in [this question](http://stackoverflow.com/q/23267975/1057616). This way, you can use both regexp aliases and non-regexp aliases in the same table with a simple database flag (`is_regexp`). Pretty sweet. – Nick Merrill Jan 29 '15 at 00:18
  • @NickMerrill plase considered adding your comment as an answer. – adopilot Sep 24 '18 at 11:50
  • `postconf-m` misses a white space. Unfortunately I don't have enough rep to fix it. :( ( https://meta.stackexchange.com/questions/77233 ) – Morty Jan 12 '21 at 06:10
3

Alias maps (virtual, local, ...) will work in combination with the regexp_table format. But if you have compiled in PCRE, then you can also use the pcre_table format.

Otherwise you should search for "Postfix catch-all" which is a bit more greedy.

mailq
  • 16,882
  • 2
  • 36
  • 66
2

I'd be inclined to solve this using recipient_delimiter.

If you don't mind using slightly different Reply-To addresses, you can set:

recipient_delimiter = +

in your config, and then mail to e.g. notification+message-988742@mysite.com (note the +) will be delivered to the notification user (assuming there are no more-specific rules/users matching notification+message-988742).

You could try setting recipient_delimiter = - (so that you could use the Reply-To headers as they are in the question) but I'm not sure how that would work with multiple instances of the delimiter on the left-hand side, and I don't have a Postfix to hand to check.

nickgrim
  • 4,336
  • 1
  • 17
  • 27
0

Create a rule using "with specific words in the sender's address." It will accept multiple words as "or" but I don't see how to do it with an "and." Perhaps this gets you far enough.

Sue
  • 1