Postfix Setup

Published: Saturday, 26 February 2011
Last modified: Monday, 15 October 2012
mail postfix

Postfix usage

Postfix is used as a daemon that will accept delivery of mail and process it. Processing mail includes storing it locally so it can be read by a pop3/imap daemon, or forwarding the mail onwards, optionally to multiple recipients.

The official website is at postfix.org.

Initial Postfix Setup

Install packages

postfix e.g. apt-get install postfix

Local mail storage

If you want to store mail locally, then these steps must be done first.

# useradd vmail

Change permission on the directory so only the vmail has access to it.

# mkdir /var/mail/vhosts
# chown vmail:vmail  /var/mail/vhosts
# cat /etc/passwd | grep vmail
vmail:x:1234:1234::/home/vmail:/bin/sh

In this case the uid and gid is 1234

virtual_uid_maps = static:1234
virtual_gid_maps = static:1234
virtual_mailbox_base = /var/mail/vhosts

Make sure the number matches the vmail user uid and gid, otherwise postfix will not have permissions to store the mail.

Make sure virtual_mailbox_base points to your chosen mail storage directory.

virtual_alias_maps = hash:/etc/postfix/virtual

Copy and forwards config will be stored in /etc/postfix/virtual

virtual_mailbox_maps = hash:/etc/postfix/vmailbox

Local mailboxes will be defined in /etc/postfix/vmailbox

Setup per domain

Add mail exchanger to DNS

Setup DNS so the mail exchanger points to the same server that postfix will be running.

To check that DNS is correct, run

$ nslookup -type=MX example.com

Non-authoritative answer:
example.com      mail exchanger = 300 mail.example.com.

The mail exchanger domain should be where postfix is running.

Setup per email

This is assuming you want to setup email only, and the user does not need a unix login to read their mail.

Modify virtual_mailbox_domains

Edit /etc/postfix/main.cf and add the domain to the list of virtual_mailbox_domains

Make sure the domain isn’t listed in mydestination, as this is for normal unix mail.

Add a mailbox

If you want to have this mail stored locally, so it can be accessed via pop3 or imap, you can add an entry into /etc/postfix/vmailbox

Uf your email looks like testuser@example.com, then add the following line:

testuser@example.com         example.com/testuser/

Remember to add the trailing slash, so mail is stored in a Mbox format usable by courier-imap.

Add forwards and copies

You can have this mail delivered locally to another existing vmailbox, and forwarded elsewhere. e.g. send a copy to gmail

Edit /etc/postfix/virtual

testuser@example.com         example@gmail.com,testuser@example.com,test2@example.com

If you only want to forward the mail to gmail, use something like

testuser3@example.com         test@gmail.com

testuser3@example.com will have no mail stored locally on the server.

Reload postfix

To make the changes take effect, rebuild the configuration. A corresponding file with extension “.db” will appear

Then reload postfix.

# postmap /etc/postfix/vmailbox
# postmap /etc/postfix/virtual
# postfix reload
postfix/postfix-script: refreshing the Postfix mail system

Testing

Send an example from an external system to the new email. If you expected mails to be delivered locally, check the directory /var/mail/vhosts/$email_domain/$email_username/. In the above example you should be checking that /var/mail/vhosts/example.com/testuser/ has new files appearing.

Check logs at /var/log/mail.log.

Setup pop3 and imap access

See Courier IMAP Setup.