Postfix with Smart Host

July 6th, 2007 Posted in Tech

Postfix is my favourite Linux mail server, I find gettng it up and running fairly straight forward. This is my quick guide to a working config that’ll accept mail destined for it from other mail servers and send mail via a smart host (eg: ISP mail server).

My preference is to use Debian/Ubuntu, so this guide is based on their semantics. Other distributions may differ in their requirements. I assume you have an otherwise running install of the OS and networking is properly configured. It also assumes your smart host doesn’t require authentication.

Install postfix and configure

First we need to edit the main configuration file:

server:/home/user# nano -w /etc/postfix/main.cf

myhostname = hostname
mydomain = domain
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
vitual_alias_maps = hash:/etc/postfix/virtual
myorigin = /etc/mailname
mydestination = domain, localhost
relayhost = smart.host.tld
mynetworks = 192.168.0.0/16, 127.0.0.0/8
home_mailbox = Maildir/

Explanation

I’m a mail server for mydomain. I serve both local and virtual users defined in the alias databases and send mail from myorigin, while accepting mail for mydestination. I send mail via the relayhost. The mynetworks setting tells me what networks I’m to accept mail for sending and I’m to deliver messages to the users as specified (ie: to their Maildir).

hostname - this is our mail server’s hostname
domain - this is our domain name
smart.host.tld - replace this with the smart host address

You’ll also need to edit /etc/mailname to reflect the domain mail is sent as. You can add other domains you accept mail for to the mydestination parameter (make sure they’re comma seperated).

User mapping and aliases

If you have email addresses that need to be mapped to usernames, you need to modify the /etc/aliases database so postfix will accept mail for the user and know where to deliver it.

server:/home/user# nano -w /etc/aliases

# /etc/aliases
postmaster: root
root: user
# add the first part of the email address up to the '@'
# symbol followed by a colon and a space then the
# local system user who is to be the recipient
email1: luser1
email2: luser2
email3: luser3

Run the following command to update the alias database:

server:/home/user# postalias /etc/aliases

If you have more than one domain or are accepting mail for a different domain than is set in /etc/mailname, you will need an /etc/postfix/virtual file to map mail for that domain to the correct user(s).

server:/home/user# nano -w /etc/postfix/virtual

user@somedomain.com localuser

The above will deliver mail for user@somedomain.com to the localuser who exists on the mailserver. This file can contain just the domain name if desirable and all mail for that domain will be delivered to the named local system user.

Run the following command to create the virtual database:

server:/home/user# postmap /etc/postfix/virtual

Finishing off

You must reload postfix after making any changes (or run the postmap/postalias commands again if editing the virtual/aliases files).

Make sure you have SMTP delivery enabled (ie: you have a MX record pointing at the external IP for your domain) and that TCP port 25 is forwarded to your mail server’s internal IP, if behind NAT.

Post a Comment