Linux:Powerful Server Administration
上QQ阅读APP看书,第一时间看更新

Adding e-mail accounts

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.

How to do it…

Follow these steps to add e-mail account:

  1. Create a new user account:
    $ useradd -s /usr/bin/nologin -m vmail
    
  2. Get the UID and GID for this account:
    $ grep vmail /etc/passwd
    vmail:x:1001:1001::/home/vmail:/usr/bin/nologin
    
  3. Create a base directory layout for domains and users:
    $ sudo mkdir -p /home/vmail/example.org/bob
    $ sudo mkdir -p /home/vmail/example.net/alice
    
  4. Allow only the user vmail to access these files:
    $ sudo chown -R vmail:vmail /home/vmail
    $ chmod -R 700 /home/vmail
    
  5. 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
  6. Create the file virtual_domains under /etc/postfix:
    $ sudo nano /etc/postfix/virtual_domains
    
    example.org
    example.net
  7. Create the virtual_maps file:
    $ sudo nano /etc/postfix/virtual_maps
    bob@example.org example.org/bob/
    alice@example.org example.org/alice/
    @example.org example.org/catchall/ # catch all address
    
  8. Create the virtual_alias file and optionally set a redirect:
    $ sudo nano /etc/postfix/virtual_alias
    # redirect emails for tim to bob
    tim@example.org  bob@example.org
  9. Now generate database of virtual maps and aliases by hashing respective files:
    $ sudo postmap /etc/postfix/virtual_maps
    $ sudo postmap /etc/postfix/virtual_alias
    
  10. 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:

  1. Create respective tables for virtual_domain, virtual_maps, and virtual_alias.
  2. Change the Postfix configuration to use MySQL as a lookup table:
    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
  3. Add the respective details to each file using the following commands:
    $ 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'
  4. You can test your mapping with the following command. This should output 1 as a result:
    $ postmap -q bob@example.org mysql:/etc/postfix/mysql-virtual-maps
    
  5. Finally, restart the Postfix daemon.
Web console for virtual mailbox administration

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.

See also