# Integrate DBMail with iRedMail (MySQL backend) on CentOS
[TOC]
## ChangeLog
* 2011-10-26: Cleanup unnecessary process for installing DBMail.
* 2011-08-27: Initial release.
## Requirements
This tutorial is written for:
* Red Hat Enterprise Linux 5.x, 6.x
* CentOS 5.x, 6.x
* Scientific Linux 6.x
## Introduce DBMail
### What DBMail is
DBMail is an open-source project that enables storage of mail messages in a relational database. Currently MySQL, PostgreSQL and SQLite can be used as storage backends. Web site: [http://www.dbmail.org/](http://www.dbmail.org/).
### Why DBMail
Quote from DBMail web site:
> * __Scalability__. DBMail is as scalable as the database system used for storage.
> * __Manageability__. DBMail can be managed by updating the relational database or directory service - without shell access.
> * __Speed__. Dbmail uses very efficient, database specific queries for retrieving mail information.
> * __Flexibility__. Changes in a Dbmail system (adding of users, changing passwords etc.) are effective immediately. Users can be stored in the database, or managed separately in an LDAP server such as OpenLDAP or Active Directory.
Notes:
* For RHEL/CentOS/Scientific Linux 5.x, we will install DBMail-2.2.17 from [yum repo `EPEL`](http://fedoraproject.org/wiki/EPEL).
* For RHEL/CentOS/Scientific Linux 6.x, we will install DBMail-3.0.0-rc from [yum repo `EPEL`](http://fedoraproject.org/wiki/EPEL).
* DBMail-2.x doesn't provide native TLS/SSL support.
* A running iRedMail server. MySQL server is installed by default in both OpenLDAP and MySQL backends, so either backend is OK. If you don't have a running iRedMail server, please follow our installation guide here: [https://docs.iredmail.org/](https://docs.iredmail.org/).
* Create MySQL database `dbmail` to store mail accounts and mail messages. Replace `password_of_dbmail` below with your password.
```sql
$ mysql -uroot -p
mysql> CREATE DATABASE dbmail DEFAULT CHARACTER SET utf8;
mysql> GRANT ALL ON dbmail.* TO dbmail@localhost IDENTIFIED BY 'password_of_dbmail';
mysql> FLUSH PRIVILEGES;
```
We now have a MySQL database `dbmail`, you can access it with MySQL user `dbmail` and password `password_of_dbmail`. You can verify it with MySQL command line:
```sql
$ mysql -udbmail -p
Enter password: # <-TypepasswordofMySQLuserdbmailhere.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dbmail | # <-Database'dbmail'isavailable.
+--------------------+
2 rows in set (0.04 sec)
```
* Stop Dovecot daemon. Since Dovecot will be replaced by DBMail, we must stop it:
```
# /etc/init.d/dovecot stop
# chkconfig --level 345 dovecot off
```
### Install DBMail
DBMail is available in [EPEL repository](http://fedoraproject.org/wiki/EPEL#How_can_I_use_these_extra_packages.3F), we will use this yum repository to install DBMail.
It's now ready to install DBMail core component and library used to connect MySQL.
On RHEL/CentOS/Scientific Linux 6.x, please make sure you have DBMail 3.0.0-rc3 or later version installed:
```
# rpm -q dbmail
dbmail-3.0.0-0.6.rc3.el6.x86_64
```
Main configure file of DBMail is `/etc/dbmail.conf` by default, we will configure it later.
DBMail provides a MySQL template file `/usr/share/doc/dbmail-x.y.z/sql/mysql/create_tables.mysql` (replace 'x.y.z' by real version number on your server), we should import it to create necessary MySQL tables to store mail accounts and messages. Here we use 'dbmail-3.0.0' for example.
DBMail daemons are running, let's create a testing account to test POP3/POP3S/IMAP/IMAPS/Managesieve services.
* Create a testing account `test@domain.ltd`, with password `mypass`:
```
# dbmail-users -p md5 -a test@domain.ltd -w mypass -s test@domain.ltd
Adding INBOX for new user... ok.
[test@domain.ltd]
Done
test@domain.ltd:x:5:0:0.00:0.00:test@domain.ltd
```
Refer to DBMail wiki site for more information about managing users: http://dbmail.org/dokuwiki/doku.php/manage_users
### Test POP3/IMAP/Managesieve services with telnet
It's OK to test POP3/POP3S/IMAP/IMAPS services with telnet, mutt or Roundcube webmail, here we use telnet and mutt instead. After testing, you can login to Roundcube Webmail directly.
#### Testing POP3 service
```
$ telnet localhost 110
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
+OK DBMAIL pop3 server ready to rock <e2ef9332465296ebd4ec7ae5828d9cc3@c60.iredmail.org> # <--DBMailresponse
USER test@domain.ltd # <-Typethiscommandtologin
+OK Password required for test@domain.ltd
PASS mypass # <--Typeyourpassword
+OK test@domain.ltd has 0 messages (0 octets)
LIST # <--Checkmailbox.
+OK 0 messages (0 octets)
.
QUIT # <--DIsconnect
+OK see ya later
Connection closed by foreign host.
```
Telnet doesn't support SSL service, so we test POP3S with `mutt` (a console based mail client application) instead.
> Aug 14 06:40:20 c60 postfix/qmgr[6016]: B89A141FAD: removed
### Configure Roundcube to work with DBMail
Change below setting in `/var/www/roundcubemail/config/main.inc.php`:
```php
$rcmail_config['imap_auth_type'] = "LOGIN";
```
Restart Apache, you can now view sent email after logging into Roundcube webmail:
```
# /etc/init.d/httpd restart
```
## Replace Dovecot with Cyrus-SASL as SMTP SASL auth daemon
Postfix uses `dovecot` as SASL type in iRedMail by default, since Dovecot will be replaced by DBMail, we cannot use Dovecot anymore. So we're going to install Cyrus-SASL libraries, and use daemon 'saslauthd' for SMTP SASL auth.
In this section, we will:
* Install Cyrus-SASL libraries.
* Configure Cyrus-SASL.
* Running and testing Cyrus-SASL auth daemon 'saslauthd' immediately.
### Install Cyrus-SASL libraries
Cyrus-SASL libraries are available in default yum repositories.
* For RHEL, they're available in yum repository `rhn` or CD/DVD images.
* For CentOS and Scientific Linux, they're available in default yum repositories.
Note: If you're running DBMail-3.0.0-rc1, you will always get `0: NO "authentication failed"`, because DBMail-3.0.0-rc1 has a compatibility issue with Cyrus-SASL.