All notes
Mail

Basics

Install postfix, dovecot

Linode: Email with postfix, dovecot and mysql.

postfix

A quote from Postfix's site on what it is:

Postfix attempts to be fast, easy to administer, and secure, while at the same time being sendmail compatible enough to not upset existing users. Thus, the outside has a sendmail-ish flavor, but the inside is completely different.
Reference.

Installation

References

Mysql setting

create database if not exists mailserver;
use mailserver;
GRANT SELECT ON mailserver.* TO 'mailuser'@'localhost' IDENTIFIED BY 'mailuser';
FLUSH PRIVILEGES;

CREATE TABLE if not exists `virtual_domains` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE if not exists `virtual_users` (
  `id` int(11) NOT NULL auto_increment,
  `domain_id` int(11) NOT NULL,
  `password` varchar(106) NOT NULL,
  `email` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`),
  FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE if not exists `virtual_aliases` (
  `id` int(11) NOT NULL auto_increment,
  `domain_id` int(11) NOT NULL,
  `source` varchar(100) NOT NULL,
  `destination` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `mailserver`.`virtual_domains`
  (`id` ,`name`)
VALUES
  ('1', 'wangchaofeng.com');

INSERT INTO `mailserver`.`virtual_users`
  (`id`, `domain_id`, `password` , `email`)
VALUES
  ('1', '1', ENCRYPT('admin', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), '[email protected]');

/etc/postfix/mysql-virtual-mailbox-domains.cf

user = mailuser
password = mailuser
hosts = localhost
dbname = mailserver
query = SELECT 1 FROM virtual_domains WHERE name='%s'

/etc/postfix/mysql-virtual-mailbox-maps.cf

user = mailuser
password = mailuser
hosts = localhost
dbname = mailserver
query = SELECT 1 FROM virtual_users WHERE email='%s'

/etc/postfix/mysql-virtual-alias-maps.cf

user = mailuser
password = mailuser
hosts = localhost
dbname = mailserver
query = SELECT destination FROM virtual_aliases WHERE source='%s'

/etc/postfix/mysql-virtual-email2email.cf

user = mailuser
password = mailuser
hosts = localhost
dbname = mailserver
query = SELECT email FROM virtual_users WHERE email='%s'

Test for mysql:

sudo service postfix restart
postmap -q wangchaofeng.com mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
postmap -q [email protected] mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
postmap -q [email protected] mysql:/etc/postfix/mysql-virtual-alias-maps.cf

Settings

Explanations

Dovecot setting

/etc/dovecot/dovecot.conf

# Enable installed protocols
!include_try /usr/share/dovecot/protocols.d/*.protocol
protocols = imap pop3 lmtp

/etc/dovecot/conf.d/10-mail.conf

mail_location = maildir:/var/mail/vhosts/%d/%n
...
mail_privileged_group = mail

mkdir /etc/dovecot/private
openssl req -new -x509 -days 1000 -nodes -out "/etc/dovecot/dovecot.pem" -keyout "/etc/dovecot/private/dovecot.pem"

Test the settings

# Find out what the machine name is:
postconf -d myhostname

sudo /etc/init.d/postfix restart

mail [email protected]
Subject: test
test
.
Cc:

LDAP for postfix

# Alias
server_host = ldap://127.0.0.1
version = 3
search_base = dc=myCorp,dc=com
bind = yes
bind_dn = cn=admin,dc=myCorp,dc=com
bind_pw = adminPasswd
query_filter = (&(objectClass=posixAccount)(uid=%u))
result_attribute = uid

# Mailbox
server_host = ldap://127.0.0.1
version = 3
search_base = dc=myCorp,dc=com
bind = yes
bind_dn = cn=admin,dc=myCorp,dc=com
bind_pw = adminPasswd
query_filter = (&(objectClass=posixAccount)(uid=%u))
result_attribute = uid
result_format = %s/mail/

sendmail

Ref.

sendmail
-i	When  reading  a  message  from standard input, donĀ“t treat a line with only a . character as the end of input.
-t	Extract recipients from message headers. These are  added  to  any xrecipients specified on the command line.

dovecot

Ref.

To configure PAM for dovecot, create /etc/pam.d/dovecot with the following content:

/etc/pam.d/dovecot
auth    required        pam_unix.so nullok
account required        pam_unix.so