27

To create a test email server, I have a similar requirement as:

How to redirect all outgoing email from postfix to a single address for testing

But I need to send all the emails to an external account, not a local one.

I would like to do something like:

  • xyz:email@gmail.com

but xyz is not local nor smtp.

user43856
  • 271
  • 1
  • 3
  • 3

3 Answers3

55

Create /etc/postfix/virtual-regexp with the following content:

/.+@.+/ email@gmail.com

Edit /etc/postfix/main.cf and add the file to virtual_alias_maps. The end result might look like this:

virtual_alias_maps = regexp:/etc/postfix/virtual-regexp

If you had existing virtual_alias_maps, separate the values with commas (eg. virtual_alias_maps = hash:/etc/postfix/virtual, regexp:/etc/postfix/virtual-regexp).

Build the mapfile by typing:

postmap /etc/postfix/virtual-regexp

Then restart postfix :

sudo service postfix restart

Voila!

BlueRaja
  • 986
  • 1
  • 10
  • 17
Mattias Ahnberg
  • 4,039
  • 18
  • 19
  • 1
    Thank you Mattias Ahnberg for your answer, How about sending multiple mails to the machine with postfix installed with one unique email address (gmail for example)? I can only receive the 2-3 first mail , the rest is bounced. – postmaps Jun 01 '13 at 20:13
  • Is it possible to redirect all mail addressed to `local` users (root etc.) to some external address and not redirect other mail? I think the at sign should be negated somehow – basin May 15 '15 at 14:05
  • @basin Is seems like replacing email@gmail.com with root@localhost would do that, no? – user14645 Jun 23 '15 at 15:47
  • No, I guess this answers: http://serverfault.com/questions/318426/postfix-virtual-domains-how-to-accept-all-subdomains-except-for-two – basin Jun 23 '15 at 21:48
  • Regular expression tables are used as is, there’s no point in running postmap on them. – Joó Ádám Jul 23 '15 at 21:06
  • 2
    In newer postfix versions the setting might need to be `virtual_alias_maps` instead of `virtual_maps` – Lienhart Woitok Mar 05 '20 at 16:09
  • @LienhartWoitok I upvoted your comment but it seems to be wrong. To use regexp you need to user virtual_maps, I've juste tried on a server. – Nico Oct 27 '20 at 18:29
  • No @LienhartWoitok is correct, `virtual_maps` is deprecated. See [the docs](http://www.postfix.org/postconf.5.html#virtual_maps). I've updated the post with the latest correct info. – BlueRaja May 31 '21 at 00:08
3

As this took me some time:

If you want to exclude target domains from this, prefix the rule with

/.+@exclude1.com/ @exclude1.com
/.+@exclude2.com/ @exclude2.com
...

to preserve original recipient, i found the virtual manpage a bit confusing on how to achive this.

-4

Is it acceptable to just have it go to a local account that then forward all mail to your external address? You could get it running like the other question you mentioned, and then use procmail to have the messages forwarded and then deleted locally. Something like this would probably do the trick by replacing the folder with /dev/null.

Paul Kroon
  • 2,220
  • 16
  • 20