How-To: Virtual emails accounts with Postfix and Dovecot — page 3

2 minute read

Following Postfix configuration, it is now time to configure Dovecot.

5. Configuring Dovecot

Dovecot has its SQL configuration gathered in a separated file: /etc/dovecot/dovecot-sql.conf, so let’s edit edit with our settings:

# vi /etc/dovecot/dovecot-sql.conf
driver = mysql
connect = host=127.0.0.1 dbname=virtual_email user=vemailuser password=vemailpass
default_pass_scheme = PLAIN-MD5
password_query = SELECT password FROM virtual_users AS V LEFT JOIN virtual_domains AS D ON V.domain_id=D.id WHERE V.user='%n' AND D.name='%d'

this will take care of getting the password from the database for a given user@domain.

Now, comes the biggest part of the config in /etc/dovecot/dovecot.conf. In this case, we are going to only enable IMAPS and the emails will be stored in /var/vmail/domain/user/Maildir.

You need to edit your conf file so it looks like:

# vi /etc/dovecot/dovecot.conf
protocols = imaps
mail_location = maildir:/var/vmail/%d/%n/Maildir

## uncomment this if you already have email from
## courier for instance.

#namespace private {
#  separator = .
#  prefix = INBOX.
#  inbox = yes
#}
## change section "protocol lda" to:
protocol lda {
  # Address to use when sending rejection mails.
  postmaster_address = [email protected]
  log_path = /var/vmail/dovecot-deliver.log
  # Hostname to use in various parts of sent mails, eg. in Message-Id.
  # Default is the system's real hostname.
  #hostname = 

  # Support for dynamically loadable plugins. mail_plugins is a space separated
  # list of plugins to load.
  #mail_plugins = 
  #mail_plugin_dir = /usr/lib/dovecot/modules/lda

  # Binary to use for sending mails.
  #sendmail_path = /usr/lib/sendmail

  # UNIX socket path to master authentication server to find users.
  auth_socket_path = /var/run/dovecot/auth-master

  # Enabling Sieve plugin for server-side mail filtering
  # handy for storing spam in their folders
  mail_plugins = cmusieve
  global_script_path = /var/vmail/globalsieverc
}


## in section auth default
## change :
mechanisms = plain login
## comment out "passdb pam"
## and make sure the following is in
## to look for users in the DB
  passdb sql {
    # Path for SQL configuration file, see /etc/dovecot/dovecot-sql.conf for example
    args = /etc/dovecot/dovecot-sql.conf
  }

## and add this so dovecot does not deal with uid/gid
## we use uid and gid 5000 for everybody
  userdb static {
    args = uid=5000 gid=5000 home=/home/vmail/%d/%n allow_all_users=yes
  }

## next make sure the section "socket listen" looks like this
## so dovecot and postfix work happily together
  socket listen {
     master {
       path = /var/run/dovecot/auth-master
       mode = 0600
       user = vmail # User running Dovecot LDA
       #group = mail # Or alternatively mode 0660 + LDA user in this group
     }
     client {
      # The client socket is generally safe to export to everyone. Typical use
      # is to export it to your SMTP server so it can do SMTP AUTH lookups
      # using it.
      path = /var/spool/postfix/private/auth
      #path = /var/run/dovecot/auth-client
      mode = 0660
      user = postfix
      group = postfix
     }
   }

Optionally we can create a system wide sieve rule that will move spam the Spam folder:

# vi /var/vmail/globalsieverc
require ["fileinto"];
# Move spam to spam folder
if anyof(header :contains "X-Spam-Flag" ["YES"], header :contains "X-DSPAM-Result" ["Spam"]) {
  fileinto "Spam";
  stop;
}

And finally, we need to set the appropriate rights on the dovecot conf files:

# chgrp vmail /etc/dovecot/dovecot.conf
# chmod g+r /etc/dovecot/dovecot.conf

6. Restarting the services

Now, everything should be good, and restarting postfix and dovecot get our setting working:

# /etc/init.d/postfix restart
# /etc/init.d/dovecot restart

If issues were to come, well…. /var/log/mail.log is the place to check.

7. References

  • http://workaround.org/articles/ispmail-etch/