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.
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.
Follow these steps to add e-mail account:
$ useradd -s /usr/bin/nologin -m vmail
$ grep vmail /etc/passwd vmail:x:1001:1001::/home/vmail:/usr/bin/nologin
$ sudo mkdir -p /home/vmail/example.org/bob $ sudo mkdir -p /home/vmail/example.net/alice
$ sudo chown -R vmail:vmail /home/vmail $ chmod -R 700 /home/vmail
/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
virtual_domains
under /etc/postfix
:$ sudo nano /etc/postfix/virtual_domains
example.org example.net
virtual_maps
file:$ sudo nano /etc/postfix/virtual_maps [email protected] example.org/bob/ [email protected] example.org/alice/ @example.org example.org/catchall/ # catch all address
virtual_alias
file and optionally set a redirect
:$ sudo nano /etc/postfix/virtual_alias # redirect emails for tim to bob [email protected] [email protected]
$ sudo postmap /etc/postfix/virtual_maps $ sudo postmap /etc/postfix/virtual_alias
$ sudo postfix reload $ sendmail [email protected]
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 [email protected]
, which can be checked by the domain administrator.
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:
virtual_domain
, virtual_maps
, and virtual_alias
.virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-domains virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-maps virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias
$ sudo nano /etc/postfix/mysql-virtual-domains
user = mysql_user
password = mysql_password
hosts = 127.0.0.1
dbname = mysql_db_name
query = SELECT 1 FROM virtual_domains WHERE name='%s'
$ sudo nano /etc/postfix/mysql-virtual-maps
...
query = SELECT 1 FROM virtual_users WHERE email='%s'
$ sudo nano /etc/postfix/mysql-virtual-alias
... query = SELECT destination FROM virtual_aliases WHERE source='%s'
1
as a result:$ postmap -q [email protected] mysql:/etc/postfix/mysql-virtual-maps
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.