Mail forwarding with Postfix

Postfix is a commonly used MTA (Mail Transfer Agent) program that can receive, deliver or route emails. So if you have your own domain and a server then you can use postfix to setup emails on your domain like [email protected].

But having a mail server on your own is a lot of work, you not only have to configure the server to receive all mails, often you use a database for this, you also have to think about spam, virus detection and security.

Forwarding your mails to a third party mail server resolves the hassle mentioned above. The third party server handles all the security issues and spam detection.

So if you just want to use your own mail adresses is much easier to just forward them. In this tutorial you will learn all the steps required to achive this. This tutorial covers debian or ubuntu distributions but it should be the same on other linux distributions

1. Install Postfix

First of all you have to install postfix on your server. This replaces sendmail as mail transfer agent.

# sudo apt update
# sudo apt install postfix

During the installation you will be asked a few questions. Choose “satellite system” and insert your data.

After the installation has completed you can test it with following command:

# sudo service postfix status

The output should be similar like following:

postfix.service - Postfix Mail Transport Agent
     Loaded: loaded (/lib/systemd/system/postfix.service; enabled; vendor prese>
     Active: active (exited) since Sun 2021-10-10 17:41:38 CEST; 19h ago
   Main PID: 5779 (code=exited, status=0/SUCCESS)
      Tasks: 0 (limit: 60)
     Memory: 0B
     CGroup: /system.slice/postfix.service

2. Configure Postfix

For the next stage you have to tell Postfix which mails have to be forwarded to which address. Therefore you have to edit the main.cf in the directory /etc/postfix

# cd /etc/postfix
# nano main.cf

Add the following line if not present at the end of the file. This is needed if you have to use ipv4 for the targeting mail server (e.g. googlemail.com)

inet_protocols = ipv4

Now insert the next two lines at the end and safe the file. You can add more domains by seperating them with a blank space

virtual_alias_domains = yourdomain1.com yourdomain2.com
virtual_alias_maps = hash:/etc/postfix/virtual

Create a new file called “virtual” and insert the mail addresses you like to forward.
The first email is the address on which postfix shall receive emails, and the second is the address where postfix would forward the emails.

  • To forward to two email addresses
[email protected] [email protected]
[email protected] [email protected]
  • The mail can be forwarded to multiple destinations
[email protected] [email protected] [email protected]
  • To catch and forward emails to any address for a given domain, use the following notation
@yourdomain.com [email protected]

Now you must create the virtual database for Postfix. This easy step is done with the command:

# postmap /etc/postfix/virtual

2. Restart and test Postfix

At the end you have to tell Postfix to stop and start again. I experienced some problems with just reloading Postfix thats why I prefer stoping and starting the mail agent.

# service postfix stop
# service postfix start

At last make a test and send a test email to check if forwarding works correct.
If you encounter errors you can check whats going on in /var/log/mail. log and /var/log/mail.err

Please let me know if everything was working for you or if you encountered problems.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Du kannst diese HTML-Tags und -Attribute verwenden: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>