postfix
- apt-get install postfix postfix-mysql dovecot-core dovecot-imapd dovecot-mysql mysql-server dovecot-lmtpd postgrey
在的配置菜单里,选择Internet Site,设置这个系统的邮件名称为linuxidc.net。
现在开始添加一个数据库用于保存主机上管理的域名列表,和每个域名下的用户列表(同时也包括他们各自的密码),以及邮件别名列表(用于从一个地址往另一个地址转发邮件)。
- mysqladmin -p create mailserver
- mysql -p mailserver
- mysql> GRANT SELECT ON mailserver.* TO 'mailuser'@'localhost' IDENTIFIED BY 'mailuserpass';
- mysql> FLUSH PRIVILEGES;
- mysql> CREATE TABLE `virtual_domains`(
- `id`int(11) NOT NULL auto_increment,
- `name` varchar(50) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- mysql> CREATE TABLE `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;
- mysql> CREATE TABLE `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;
这里我们为linuxidc.net域名提供邮件服务。如果还需要加入其他域名,也没问题。我们也会为每个域名设置一个邮件管理地址(postmaster),转寄给。
- mysql> INSERT INTO virtual_domains (`name`) VALUES ('linuxidc.net');
- mysql> INSERT INTO virtual_domains (`name`) VALUES ('otherdomain.net');
- mysql> INSERT INTO virtual_aliases (`domain_id`,`source`,`destination`) VALUES('1','postmaster','roudy@linuxidc.net');
- mysql> INSERT INTO virtual_aliases (`domain_id`,`source`,`destination`) VALUES('2','postmaster','roudy@linuxidc.net');
现在已经添加了一个本地邮件账号。首先,为它生成一个密码的哈希串:
- doveadm pw -s SHA512-CRYPT
然后把哈希值加入到数据库中:
- mysql> INSERT INTO `mailserver`.`virtual_users`(`domain_id`,`password`,`email`) VALUES ('1','$6$YOURPASSWORDHASH','roudy@linuxidc.net');
现在我们的域名、别名和用户列表都设置好了,然后开始设置postfix(这是一个SMTP服务器,用来发送邮件)。把文件/etc/postfix/main.cf替换为下面的内容:
- myhostname = cloud.linuxidc.net
- myorigin =/etc/mailname
- mydestination = localhost.localdomain, localhost
- mynetworks_style = host
- # We disable relaying in the general case
- smtpd_recipient_restrictions = permit_mynetworks, reject_unauth_destination
- # Requirements on servers that contact us: we verify the client is not a
- # known spammer (reject_rbl_client) and use a graylist mechanism
- # (postgrey) to help reducing spam (check_policy_service)
- smtpd_client_restrictions = permit_mynetworks, reject_rbl_client zen.spamhaus.org, check_policy_service inet:127.0.0.1:10023
- disable_vrfy_command = yes
- inet_interfaces = all
- smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
- biff =no
- append_dot_mydomain =no
- readme_directory =no
- # TLS parameters
- smtpd_tls_cert_file=/etc/ssl/certs/cloud.crt
- smtpd_tls_key_file=/etc/ssl/private/cloud.key
- smtpd_use_tls=yes
- smtpd_tls_auth_only = yes
- smtp_tls_security_level=may
- smtp_tls_loglevel =1
- smtpd_tls_loglevel =1
- smtpd_tls_received_header = yes
- smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
- smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
- # Delivery
- alias_maps = hash:/etc/aliases
- alias_database = hash:/etc/aliases
- message_size_limit =50000000
- recipient_delimiter =+
- # The next lines are useful to set up a backup MX for myfriendsdomain.org
- # relay_domains = myfriendsdomain.org
- # relay_recipient_maps =
- # Virtual domains
- virtual_transport = lmtp:unix:private/dovecot-lmtp
- virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
- virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
- virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias-maps.cf
- local_recipient_maps = $virtual_mailbox_maps
现在我们要让postfix知道如何从我们设定的数据库里找出需要接收邮件的域名。建立一个新文件/etc/postfix/mysql-virtual-mailbox-domains.cf并添加以下内容:
- user = mailuser
- password = mailuserpass
- hosts =127.0.0.1
- dbname = mailserver
- query = SELECT 1 FROM virtual_domains WHERE name='%s'
我们可以让postfix判断给定的电子邮件账号是否存在,创建文件/etc/postfix/mysql-virtual-mailbox-maps.cf并写入以下内容:
- user = mailuser
- password = mailuserpass
- hosts =127.0.0.1
- dbname = mailserver
- query = SELECT 1 FROM virtual_users WHERE email='%s'
最后,postfix会根据文件/etc/postfix/mysql-virtual-alias-maps.cf的内容来查找邮件别名
- user = mailuser
- password = mailuserpass
- hosts =127.0.0.1
- dbname = mailserver
- query = SELECT virtual_aliases.destination as destination FROM virtual_aliases, virtual_domains WHERE virtual_aliases.source='%u' AND virtual_aliases.domain_id = virtual_domains.id AND virtual_domains.name='%d'
在配置好这些后,现在要测试一下postfix是否能正常查询数据库。我们可以用postmap命令测试:
- postmap -q linuxidc.net mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
- postmap -q roudy@linuxidc.net mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
- postmap -q postmaster@linuxidc.net mysql:/etc/postfix/mysql-virtual-alias-maps.cf
- postmap -q bob@linuxidc.net mysql:/etc/postfix/mysql-virtual-alias-maps.cf
如果一切都正常配置了的话,头两个查询应该输出1,第3个查询应该输出,而最后一个应该什么都不输出。