Configure Postfix, PostfixAdmin, Dovecot & SMTP-Auth SASL Using MySQL on Ubuntu 12.04 LTS

Set up MySQL For Virtual Domains and Users’ Mailboxes

There are a couple of choices to be made here you can either do this from the MySQL shell, or you could use PHPMyAdmin to administer the MySQL tables.

Using the MySQL Shell

To start the MySQL Shell type the following :

mysql -u root -p

the command will prompt you for the password you selected during the MySQL package installation.  The following will be shown :

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is xx
Server version 5.5.24-0ubuntu0.12.04.1 (Ubuntu)

Copyright (c) 2000,2011, Oracle and/or its affiliates.  All rights 
reserved.

Oracle is a registered trademark of Oracle Corporation and/or its 
affiliates.  Other names may be trademarks of their respective owners.

Type 'help;' or '\h'.  Type '\c' to clear current input statement.

mysql> _

Issuing the following command will create a database that postfix can use for all of its configurations.

CREATE DATABASE postfix;
USE postfix;

Next, a mail service account is needed which is a special user account used by postfix to administer the database, and the tables it contains.  In this example the userid is simply called postfix, and the postfix_admin password needs to be of significant complexity.  As follows :

CREATE USER 'postfix'@'localhost' IDENTIFIED BY 'postfix_complex_password';

The next few lines will grant the various permission to allow this user account access to the postfix database and its tables.

GRANT SELECT, INSERT, UPDATE, DELETE on postfix.* TO 'postfix'@'localhost' IDENTIFIED BY 'postfix_password';
GRANT SELECT, INSERT, UPDATE, DELETE on postfix.* TO 'postfix'@'localhost.localdomain' IDENTIFIED BY 'postfix_password';
FLUSH PRIVILEGES;

It time to create a few tables within this database for postfix :

CREATE TABLE `mailbox` (
    `username` varchar(255) COLLATE latin1_general_ci NOT NULL,
    `password` varchar(255) COLLATE latin1_general_ci NOT NULL,
    `name` varchar(255) CHARACTER SET utf8 NOT NULL,
    `maildir` varchar(255) COLLATE latin1_general_ci NOT NULL,
    `quota` bigint(20) NOT NULL DEFAULT '0',
    `local_part` varchar(255) COLLATE latin1_general_ci NOT NULL,
    `domain` varchar(255) COLLATE latin1_general_ci NOT NULL,
    `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
    `modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
    `active` tinyint(1) NOT NULL DEFAULT '1',
    PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

The next MySQL statement sets up the aliases for the mailboxes :

CREATE TABLE `alias` (
    `address` varchar(255) COLLATE latin1_general_ci NOT NULL,
    `goto` text COLLATE latin1_general_ci NOT NULL,
    `domain` varchar(255) COLLATE latin1_general_ci NOT NULL,
    `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
    `modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
    `active` tinyint(1) NOT NULL DEFAULT '1',
    PRIMARY KEY (`address`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

Create a table for vacational information, if a user wants to state they are on holiday :

CREATE TABLE `vacation` (
  `email` varchar(255) COLLATE latin1_general_ci NOT NULL DEFAULT '',
  `subject` varchar(255) COLLATE latin1_general_ci NOT NULL DEFAULT '',
  `body` text COLLATE latin1_general_ci NOT NULL,
  `cache` text COLLATE latin1_general_ci NOT NULL,
  `domain` varchar(255) COLLATE latin1_general_ci NOT NULL DEFAULT '',
  `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `active` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

The next statement will create the table which contains the setting for virtual domains :

CREATE TABLE `domain` (
  `domain` varchar(255) COLLATE latin1_general_ci NOT NULL,
  `description` varchar(255) CHARACTER SET utf8 NOT NULL,
  `aliases` int(10) NOT NULL DEFAULT '0',
  `mailboxes` int(10) NOT NULL DEFAULT '0',
  `maxquota` bigint(20) NOT NULL DEFAULT '0',
  `quota` bigint(20) NOT NULL DEFAULT '0',
  `transport` varchar(255) COLLATE latin1_general_ci NOT NULL,
  `backupmx` tinyint(1) NOT NULL DEFAULT '0',
  `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `active` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`domain`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

Exit the mysql command line processor by typing QUIT. This concludes setting up the MySQL tables required for postfix and dovecot to process virtual domains, and their associated mailboxes.

5 thoughts on “Configure Postfix, PostfixAdmin, Dovecot & SMTP-Auth SASL Using MySQL on Ubuntu 12.04 LTS

  1. AAAaarrgh, SOOOooo close 🙂
    This whole setup was going along absolutely Bang On. Just systematic clockwork, then couldn’t connect at the testing phase.
    Great Post though. I’m really surprised no one else has posted on this tut. as it is the perfect setup in my mind.

    Question for the owner… Could you put the versions of the packages you’re using up for us? I’ll still have to work out how to install them on my system, but that would be a great help. Dovecot upgrades are proving a real hassle to a lot of us. Specially without updated tuts.

    Again, great post, hope to hear about those package versions too.

    Thanx

  2. Thanks for your support, I know it’s a challenge to get all these components to work. TBH I didn’t pay too much attention to the package version, just got the standard versions of the packages that come with the Ubuntu 12.04 LTS Server. I have broken them down as follows hope you manage to get you installation working soon:
    ii dovecot-common 1:2.0.19-0ubuntu2 Transitional package for dovecot
    ii dovecot-imapd 1:2.0.19-0ubuntu2 secure IMAP server that supports mbox, maildir, dbox and mdbox mailboxes
    ii dovecot-mysql 1:2.0.19-0ubuntu2 MySQL support for Dovecot
    ii dovecot-pop3d 1:2.0.19-0ubuntu2 secure POP3 server that supports mbox, maildir, dbox and mdbox mailboxes
    ii libmailutils2 1:2.2+dfsg1-5 GNU Mail abstraction library
    ii libpam-mysql 0.7~RC1-4build3 PAM module allowing authentication from a MySQL server
    ii libsasl2-2 2.1.25.dfsg1-3ubuntu0.1 Cyrus SASL – authentication abstraction library
    ii libsasl2-modules 2.1.25.dfsg1-3ubuntu0.1 Cyrus SASL – pluggable authentication modules
    ii libsasl2-modules-sql 2.1.25.dfsg1-3ubuntu0.1 Cyrus SASL – pluggable authentication modules (SQL)
    ii mailutils 1:2.2+dfsg1-5 GNU mailutils utilities for handling mail
    ii mysql-client 5.5.31-0ubuntu0.12.04.1 MySQL database client (metapackage depending on the latest version)
    ii mysql-client-5.5 5.5.31-0ubuntu0.12.04.1 MySQL database client binaries
    ii mysql-client-core-5.5 5.5.31-0ubuntu0.12.04.1 MySQL database core client binaries
    ii mysql-server 5.5.31-0ubuntu0.12.04.1 MySQL database server (metapackage depending on the latest version)
    rc mysql-server-5.1 5.1.63-0ubuntu0.11.10.1 MySQL database server binaries and system database setup
    ii mysql-server-5.5 5.5.31-0ubuntu0.12.04.1 MySQL database server binaries and system database setup
    ii mysql-server-core-5.5 5.5.31-0ubuntu0.12.04.1 MySQL database server binaries
    ii openssl 1.0.1-4ubuntu5.8 Secure Socket Layer (SSL) binary and related cryptographic tools
    ii postfix 2.9.3-2~12.04.2 High-performance mail transport agent
    ii postfix-mysql 2.9.3-2~12.04.2 MySQL map support for Postfix
    ii postfix-policyd-spf-perl 2.009-1 Simple Postfix policy server for RFC 4408 SPF checking
    ii python-openssl 0.12-1ubuntu2 Python wrapper around the OpenSSL library
    ii sasl2-bin 2.1.25.dfsg1-3ubuntu0.1 Cyrus SASL – administration programs for SASL users database
    ii telnet 0.17-36build1 The telnet client

  3. You have a bug in your postconf section:

    This line breaks the authentication with an error about the mailbox. “sudo postconf -e ‘virtual_mailbox_maps = /etc/postfix/mysql_virtual_mailbox_maps.cf'”

    Update it to include the proxy:mysql and it works.

    sudo postconf -e ‘virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf’

    Great document.

  4. I found this guide when trying to set up my own mail server. There are a few errors in it, however, and I’m only discovering them through encountering issues in my set up – so although I’ve noticed a couple there are maybe others.

    The first thing I’d say to people reading this is that you will need to use sudo a lot or else log in as root. If you mentioned that I didn’t notice it. When copying and pasting it is handy if you have a document ready with your usernames, passwords and the word sudo in there so you can copy/paste from this guide to a doc to your terminal.

    Next there is an issue with the line

    postconf -e ‘virtual_mailbox_maps = /etc/postfix/mysql_virtual_mailbox_maps.cf’

    which should be

    postconf -e ‘virtual_mailbox_maps = hash:/etc/postfix/mysql_virtual_mailbox_maps.cf’

    note the word “hash” above.

    Secondly you create the document
    etc/postfix/mysql_virtual_domains_map.cf

    when you should create
    mysql_virtual_domains_maps.cf

    Note the ‘s’, maps. Alternatively you should link to ‘map’ instead of ‘maps’ later in the guide.

    There may be others, it’s more than likely that I’ll not come back to update this post if I find more so beware, however, this guide can carry you most of the way.

    Sometimes when I am setting up a server or something I’ll take notes and think to myself that I will write a blog post about it some time. Then I come across an error, spend half the day fixing it and when I get around to writing the blog article I realise that having not taken proper notes when fixing the error my guide becomes useless to others. This is the sort of thing that’s happened here, you can tell by the way you wrote out the last page and how you start trying to debug your dovecot when it’s quite likely it was actually a postfix error that was catching you out.

  5. Thank you to all for taking time to point out any errors, I have double checked my own configuration and have found the line should read:

    postconf -e ‘virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf’

    I not sure it should read with a hash: prefix, as its not a hash file, but contains the SQL to query the virtual mailboxes.

    Where it states create a file :/etc/postfix/mysql_virtual_domains_map.cf this was a typo, I have now corrected this in the article, sorry if it caused any confusion, but the article should be now correct.

    This can be a challenge setting this up, but I am really keen that if people run into problems that they leave comments so that I change and improve this article for everyone.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.