In this recipe, we will learn how to add e-mail accounts to Postfix. The easiest way to add a new e-mail account to Postfix is to add a new user account on your server. Postfix will check for user accounts and deliver e-mails to respective users. We will create a virtual user setup so that we do not need to create user accounts for each e-mail user.
Getting ready
You will need access to a root account or an account with sudo privileges.
I assume that you have completed your basic Postfix setup and that it is working properly.
Next, configure Postfix. Edit /etc/postfix/main.cf and add the following lines:
virtual_mailbox_base = /home/vmail
virtual_mailbox_domains = /etc/postfix/virtual_domains
virtual_mailbox_maps = hash:/etc/postfix/virtual_maps
virtual_alias_maps = hash:/etc/postfix/virtual_alias
virtual_uid_maps = static:1001 # user ID for user vmail
virtual_gid_maps = static:1001 # group ID for user vmail
Create the file virtual_domains under /etc/postfix:
Reload Postfix and send an e-mail to the newly created address:
$ sudo postfix reload$ sendmail bob@example.org
How it works…
Here, we have created a virtual mailbox setup to enable our Postfix server to serve multiple domains as well as add e-mail users without creating user accounts on the server. All e-mails received by virtual users will be stored under the home directory of the vmail user (virtual_mailbox_base in Postfix configuration). When you need to add a new e-mail account, simply add the e-mail address with its respective domain to the virtual_maps file. In case you need to support a new domain, you can easily add it to the virtual_domains file.
The third file we used is virtual_alias. You can set e-mail forwarding in this file. It is handy when you need to create a new alias for an e-mail address or forward e-mails to one or multiple accounts. We have set a catchall entry in the virtual_alias file; this setting will redirect all e-mails received on nonexistent accounts to catchall@example.org, which can be checked by the domain administrator.
There's more…
Using files for virtual users and domains is good for getting started with setup. But once you need to add more and more user accounts and domains it is a good idea to move the users and domains to a database server. This can be easily done by changing the lookup table type. Postfix supports a variety of lookup table types, which include LDAP, MySQL, PGSQL, memcache, SQLite, and many others.
To use MySQL as a backend database, complete the following steps:
Create respective tables for virtual_domain, virtual_maps, and virtual_alias.
Change the Postfix configuration to use MySQL as a lookup table:
The Vimbadmin package provides a web console for virtual mailbox administration. It is a PHP-based open source package. You can get source code and installation instructions at https://github.com/opensolutions/ViMbAdmin.