Context: let's say I have the domain example.com, but the registrar doesn't provide an "email forward" system.

How can I configure postfix (or even a simple Python script would be better, because I could customize certain forwarding rules) to use my server of IP as a server that only redirects every mail incoming to *@example.com ("catch-all") to myadress@gmail.com ?

The DNS record would probably be something like:

mx.example.com                   MX     mailforwarder.example.com
mailforwarder.example.com        A
  • MX record is not IP but DNS name. Anyway you don't need MX, there's fallback to A record of the domain itself. I won't go with `sendmail', it's too big piece of mail server software for managing just one own domain. Check online with mxtoolbox.com. If you want something super easy, check opensmtpd.org, a mail server from OpenBSD OS which is super easy to setup.
    – Jiri B
    Mar 22, 2021 at 9:49
  • Thanks @JiriB! It would be wonderufl if you can post an answer with a few steps about how to do this (*@example.com => redirected to example@gmail.com) with OpenSMTPD (I use Debian).
    – Basj
    Mar 22, 2021 at 10:08
  • There is one caveat: GMail verifies SPF records. If you forward mails, you will encounter domains that have SPF records set. If you just forward the mails without rewriting the from-address, GMail will reject those mails. You can however install a simple mailbox system (e.g. some pop3 daemon) running on your server and configure gmail to collect those mails: support.google.com/mail/answer/… - if that solution is OK for you, I can post an answer with detailed instructions.
    – Marcel Noe
    Sep 14, 2021 at 9:24

1 Answer 1


Just setup MX in DNS properly (without the useless clutter) and verify with:

dig example.com mx

Then this can be set up with postfix:

A) Create file /etc/postfix/virtual:

@example.com example@gmail.com

Alternatively, one can also forward per mailbox:

info@example.com example@gmail.com

B) Then create the alias mapping with:

postmap /etc/postfix/virtual

C) Then in file /etc/postfix/main.cf, the alias mapping needs to be referrenced:

virtual_alias_domains = example.com test.com
virtual_alias_maps = hash:/etc/postfix/virtual

D) Then reload the server with:

systemctl reload postfix
  • dig example.com mx does not set up a MX record, it displays it if it exists. Maybe you forgot to paste in the example output?
    – telcoM
    Sep 12, 2021 at 23:26
  • It is good to verify the MX record/s on a terminal. Or just use online DNS/MX tools to validate, if you should not feel confident with that. And I mean, when reading the title of the question, it doesn't make the least sense, simply because this isn't the purpose of a MX (mail exchanger) record in the DNS. And when having several MX records (usually there are at least two of them, for minimum redundancy), these forwards would need to be configured on each destination host. Sep 13, 2021 at 4:17
  • If it would be possible, that it could work as you'd imagine it to work... this would be highly insecure, because one could siphon emails by simple DNS poisoning - and alike I've explained, the mail needs to be delivered first (the DNS should also have DKIM & SPF records, so that the identify of the MX can be verified), before they're being forwarded. Hope this explains it better. Sep 13, 2021 at 4:25
  • One can also load letsencrypt.org SSL certificates into postfix, simply because MX, DKIM, SPF (in DNS) and SSL encryption are quite the default configuration for a mail-server these days. And while you don't provide the zone file, I probably won't explain DNS to you, because it's a complex topic where one easily could write a book about it. Sep 13, 2021 at 5:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.