Forwarding Email Using Postfix

You are here:
< All Topics

How to forward incoming email for one user to another using postfix email server

 

 

postfix provides a method of redirect mail to another user for both local and remote users. You need to configure /etc/postfix/aliases (or use /etc/aliases) file.

 

The aliases file (read as table) provides a system-wide mechanism to redirect mail for local recipients. The redirections are processed by the Postfix local delivery agent.

 

Normally, the aliases table is specified as a text file that serves as input to the postalias command. The result, an indexed file in dbm or db format, is used for fast lookup by the mail system. Execute the command newaliases in order to rebuild the indexed file after changing the Postfix alias database.

 

 

Find the path of the configuration directory using the postconf command

 

$ postconf | grep config_directory

 

 

root@gemini:~# postconf | grep config_directory
config_directory = /etc/postfix
root@gemini:~#

 

 

Edit the main.cf file

 

nano /etc/postfix/main.cf

 

add the following lines at the end of it

 

virtual_alias_domains = mydomain.com myanotherdomain.com
virtual_alias_maps = hash:/etc/postfix/virtual

 

 

so in our case we do:

 

virtual_alias_domains = kevwells.com
virtual_alias_maps = hash:/etc/postfix/virtual

 

 

The first line virtual_alias_domains lists the domains, for which postfix is going to accept emails. Multiple domains are added separated by a space.

 

The second line virtual_alias_maps specifies the path to the file which is going to contain mappings specifying how to forward emails for the above domains.

 

 

Now open the

 

nano /etc/postfix/virtual

 

(create one if it does not exist) and add to it the emails you want to forward along with the destination emails.

 

eg lets forward 2 emails

 

contact@mydomain.com myself@gmail.com
sales@mydomain.com myself@gmail.com

 

The first email is the address on which postfix shall receive emails, and the second is the address where postfix would forward the emails.

 

The mail can be forwarded to multiple destinations

 

contact@mydomain.com myself@gmail.com mystaff@gmail.com

 

 

To forward all emails using postfix:

 

To catch and forward emails to any address for a given domain, use the following notation

 

# forward all emails

 

@mydomain.com myself@gmail.com mystaff@gmail.com

 

 

so in my case:

 

@kevwells.com kevrwells@gmail.com

 

 

After entering the forwarding rules, save the file and close it.

 

Then run the following commands for the new settings to take effect

 

Update the postfix lookup table

 

 

root@gemini:~# postmap /etc/postfix/virtual
root@gemini:~#

 

then open file /etc/postfix/aliases

 

 

nano /etc/postfix/aliases

 

 

root@gemini:~# newaliases
root@gemini:~#

 

Now add line as follows:

 

To redirect root email to tom user:
root: tom

Or
root: tom@example.com

 

then run the newaliases command to rebuild the aliases database:

 

newaliases

 

 

 

To troubleshoot problems see postfix server log file:

 

tail -f /var/log/mail.log

 

 

then restart postfix

 

systemctl restart postfix

 

 

root@gemini:~# systemctl restart postfix
root@gemini:~# systemctl status postfix
● postfix.service – Postfix Mail Transport Agent
Loaded: loaded (/lib/systemd/system/postfix.service; enabled; vendor preset: enabled)
Active: active (exited) since Mon 2022-03-14 10:02:23 UTC; 8s ago
Process: 148469 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 148469 (code=exited, status=0/SUCCESS)

 

Mar 14 10:02:23 gemini systemd[1]: Starting Postfix Mail Transport Agent…
Mar 14 10:02:23 gemini systemd[1]: Finished Postfix Mail Transport Agent.
root@gemini:~#

 

 

and do a check with

 

Check with the postconf command that the domain aliases and alias file have been setup properly.

 

$ postconf -n | grep virtual

 

root@gemini:~# postconf -n | grep virtual
virtual_alias_domains = kevwells.com
virtual_alias_maps = hash:/etc/postfix/virtual
root@gemini:~#

 

 

then test by sending a mail

 

Now that all configuration is complete, you can test the mail forwarding.

 

Try sending an email from somewhere outside the world, to the address on your domain, and you should see the same mail forwarded to the gmail account.

 

You should see the email arrive in the forwarded destination within a few seconds. For further diagnosis check postfix log files and check for details on how the mail was forwarded and whether it was successful or not.

 

tip
if you do postconf you get detailed system status info of postfix current variable settings

 

 
(very long list!)

 

root@gemini:/etc/postfix# cat main.cf

# Debian specific: Specifying a file name will cause the first
# line of that file to be used as the name. The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no

# appending .domain is the MUA’s job.
append_dot_mydomain = no

# Uncomment the next line to generate “delayed mail” warnings
#delay_warning_time = 4h

readme_directory = no

# See http://www.postfix.org/COMPATIBILITY_README.html — default to 2 on
# fresh installs.
compatibility_level = 2

######################################

#Enable TLS Encryption when Postfix receives incoming emails

smtpd_tls_cert_file=/etc/letsencrypt/live/mail.kevwells.com/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/mail.kevwells.com/privkey.pem
smtpd_tls_security_level=may
smtpd_tls_loglevel = 2
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache

#Enable TLS Encryption when Postfix sends outgoing emails
#Enforce TLSv1.3 or TLSv1.2

smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1
smtpd_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1

##########################################
##set in zoho below
##smtp_sasl_auth_enable = yes
##smtp_sasl_auth_enable = no

##smtp_sasl_security_options =
##smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
##smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

# TLS parameters
#smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
#smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
##smtpd_tls_security_level=may

##smtp_tls_CApath=/etc/ssl/certs
##smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

##smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination

##myhostname = gemini
myhostname = localhost

alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases

myorigin = /etc/mailname

mydomain = kevwells.com

mydestination = $myhostname, $myhostname.$mydomain

##mydestination = $myhostname, kevwells.com, gemini, localhost.localdomain, localhost

# relayhost = set below under zoho section

mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all

#virtual_alias_domains = kevwells.com
virtual_alias_maps = hash:/etc/postfix/virtual

# these two lines are for dovecot:

mailbox_transport = lmtp:unix:private/dovecot-lmtp
smtputf8_enable = no

#### this is for smtp outgoing mail server if using a relayhost for smtp####

## relayhost = [smtp.zoho.eu]:587
relayhost = smtp.gmail.com

 

# enable SASL authentication
smtp_sasl_auth_enable = yes
# location of sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
# disallow methods that allow anonymous authentication
smtp_sasl_security_options = noanonymous
# location of CA certificate
smtp_tls_CAfile = /etc/postfix/cacert.pem
# enable TLS encryption
## this is no longer valid if smtp_tls_security_level is set to a non-empty value
smtp_use_tls = yes

#### end smtp outgoing mail server ####

root@gemini:/etc/postfix#

 

 

 

Table of Contents