Configuring Postfix on Ubuntu for Universal Email Forwarding to External Address (e.g., Gmail) with Optional Spam Filtering


2 views

For a lightweight email forwarding setup on Ubuntu 9.04 (though these instructions work for modern Ubuntu versions too), edit your main Postfix configuration file:

sudo nano /etc/postfix/main.cf

Add these critical parameters:

myhostname = yourdomain.com
mydomain = yourdomain.com
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost
relayhost =
mynetworks = 127.0.0.0/8
local_recipient_maps =

To forward all incoming emails to a single address (e.g., your@gmail.com), create a transport map:

sudo nano /etc/postfix/transport

Add this line (replace with your target email):

*   smtp:[smtp.gmail.com]:587

Then compile the transport map and update main.cf:

sudo postmap /etc/postfix/transport
echo "transport_maps = hash:/etc/postfix/transport" | sudo tee -a /etc/postfix/main.cf
echo "default_transport = smtp" | sudo tee -a /etc/postfix/main.cf

For proper Gmail delivery, add SASL authentication in /etc/postfix/sasl_passwd:

[smtp.gmail.com]:587    username@gmail.com:password

Secure and activate the credentials:

sudo chmod 600 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd

Add these to main.cf:

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

For simple spam filtering before forwarding, install SpamAssassin:

sudo apt-get install spamassassin spamc
sudo systemctl enable spamassassin
sudo systemctl start spamassassin

Configure Postfix to pipe through SpamAssassin by editing master.cf:

sudo nano /etc/postfix/master.cf

Add this service definition (usually near the top):

spamassassin unix -     n       n       -       -       pipe
  user=spamd argv=/usr/bin/spamc -f -e
  /usr/sbin/sendmail -oi -f ${sender} ${recipient}

Then modify your transport map to include filtering:

*   spamassassin:smtp:[smtp.gmail.com]:587

After making changes, always test your configuration:

sudo postfix check
sudo postfix reload

Send a test email and verify delivery to your target address. Check mail logs for troubleshooting:

tail -f /var/log/mail.log

To set up email forwarding in Postfix, you'll need to modify these key configuration files:

# /etc/postfix/main.cf
myhostname = mail.yourdomain.com
mydestination = $myhostname, localhost.$mydomain, localhost
virtual_alias_maps = hash:/etc/postfix/virtual
relayhost =
inet_interfaces = all

Then create/edit the virtual aliases file:

# /etc/postfix/virtual
@yourdomain.com external@gmail.com
*@yourdomain.com external@gmail.com

Compile the aliases and restart Postfix:

sudo postmap /etc/postfix/virtual
sudo systemctl restart postfix

When forwarding to Gmail/GSuite:

  • Ensure proper SPF records (include your server's IP)
  • Configure DKIM signing (recommended but not mandatory)
  • Add your server's IP to Google's allow list if emails get marked as spam

For basic spam filtering before forwarding:

# Install SpamAssassin
sudo apt-get install spamassassin spamc

# Configure Postfix to use SpamAssassin
# Add to /etc/postfix/master.cf
smtp      inet  n       -       y       -       -       smtpd
  -o content_filter=spamassassin
spamassassin unix -     n       n       -       -       pipe
  user=spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f ${sender} ${recipient}

For more control over forwarding:

# /etc/postfix/header_checks
/^To:.*@yourdomain.com/ REDIRECT external@gmail.com

Then in main.cf:

header_checks = regexp:/etc/postfix/header_checks
  • Check mail logs: tail -f /var/log/mail.log
  • Test configuration: postconf -n
  • Verify DNS records with dig or nslookup