Postfix Mail Server configuration tutorial

09 March 2013
By Gonçalo Marques
In this tutorial you will learn how to deploy a simple configuration of Postfix Mail Server.

Introduction

Postfix is an open source MTA or SMTP server. In this tutorial we will deploy a very simple configuration of Postfix. We will see in other tutorials how we can deploy more complex configuration including user authentication, SSL and message signing.

This tutorial considers the following software and environment:

  1. Ubuntu 12.10
  2. Postfix 2.9.6

Install Postfix

We start by installing Postfix. Issue the following command in the console:

apt-get install postfix

Now we may also install a simple SMTP client like mailutils so we can test the configuration at the end:

apt-get install mailutils

Configure main.cf

One of the most important Postfix configuration files is main.cf. The file is usually located at:
/etc/postfix/main.cf.

Edit the file and add the following lines where domain1.com is your mail domain:

myhostname = domain1.com
mydestination = localhost.$mydomain, localhost

Uncomment the following lines:

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

Comment the following lines:

setgid_group =
sendmail_path =
mailq_path =
newaliases_path =
manpage_directory =
sample_directory =
readme_directory =
html_directory =

Creating mailbox owner

Now we need to configure the mailbox owner. Create the user and the respective group by issuing the following command in the console:

groupadd -g 5000 vmail
useradd -m -u 5000 -g 5000 -s /bin/bash vmail

Configuring mail directories in main.cf

We go back again to /etc/postfix/main.cf and add the following lines:

virtual_mailbox_domains = /etc/postfix/vhosts
virtual_mailbox_base = /home/vmail
virtual_mailbox_maps = hash:/etc/postfix/vmaps
virtual_minimum_uid = 1000
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
biff = no
append_dot_mydomain = no

Creating domains

Edit the file:
/etc/postfix/vhosts

Insert your mail domains in the file, one domain per line:

domain1.com

Creating mailboxes

Edit the file:
/etc/postfix/vmaps

Insert as much users you need in this file - one user per line - following the pattern:

[email protected] domain1.com/user/

All mail that is sent to [email protected] will be stored in domain1.com/user/ directory.

Generate mail database file

Now we generate the mail database file by issuing the following command in the console:

postmap /etc/postfix/vmaps

After this step you should a mail database file at:

/etc/postfix/vmaps.db

Start Postfix and testing

We may start Postfix now by issuing the following command:

service postfix start

After the service is started we may use mailutils to test if the mail server is ok. If you already installed mailutils you may issue the following command in the console:

You will be asked to insert Cc (carbon copy). You may leave it empty and press ENTER. Now insert the Subject and press ENTER again. Finally insert your message text and press ENTER to go into a new empty line and then press Ctrl+D to send the message.

If everything went OK you should have a new mail message in:

/home/vmail/domain1.com/user/new

Configuring a mail server can be problematic especially if you are doing it for the first time. Postfix has a robust logging mechanism and by looking at the logs you can almost certainly check what went wrong. The log file is located at:

/var/log/mail.log

Related Articles

Comments

About the author
Gonçalo Marques is a Software Engineer with several years of experience in software development and architecture definition. During this period his main focus was delivering software solutions in banking, telecommunications and governmental areas. He created the Bytes Lounge website with one ultimate goal: share his knowledge with the software development community. His main area of expertise is Java and open source.

GitHub profile: https://github.com/gonmarques

He is also the author of the WiFi File Browser Android application: