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
apt-get install postfix
Local mail storage
If you want to store mail locally, then these steps must be done first.
- Choose a unix user that will own the mail to be stored locally. I’ve chosen
- Create the unix user
vmail. This is the unix user that has permissions to read and write the mail files that are stored locally.
# useradd vmail
- Create the mail storage directory
/var/mail/vhostsas the directory. Each user’s mail will be stored in their own subdirectory under this directory.
Change permission on the directory so only the
vmail has access to it.
# mkdir /var/mail/vhosts # chown vmail:vmail /var/mail/vhosts
- Remember the uid and gid of the
# cat /etc/passwd | grep vmail vmail:x:1234:1234::/home/vmail:/bin/sh
In this case the uid and gid is 1234
- Edit postfix config
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.
virtual_mailbox_base points to your chosen mail storage directory.
- Add virtual_alias_maps
virtual_alias_maps = hash:/etc/postfix/virtual
Copy and forwards config will be stored in
- Add virtual_mailbox_maps
virtual_mailbox_maps = hash:/etc/postfix/vmailbox
Local mailboxes will be defined in
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.
/etc/postfix/main.cf and add the domain to the list of
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
Uf your email looks like email@example.com, then add the following line:
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
If you only want to forward the mail to gmail, use something like
firstname.lastname@example.org will have no mail stored locally on the server.
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
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
Check logs at
Setup pop3 and imap access
See Courier IMAP Setup.