Ubuntu 1904 Postfix安装教程

1、安装Postfix

sudo apt install postfix mysql-server postfix-mysql libsasl2-modules-sql

本示例使用 sasl+ mysql 实现用户认证功能。参考官方文档:http://www.postfix.org/MYSQL_README.html

2、创建数据库

create database postfix;

3、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`),
  KEY `domain_id` (`domain_id`),
  CONSTRAINT `virtual_aliases_ibfk_1` FOREIGN KEY (`domain_id`) REFERENCES `virtual_domains` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `virtual_domains` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

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`),
  KEY `domain_id` (`domain_id`),
  CONSTRAINT `virtual_users_ibfk_1` FOREIGN KEY (`domain_id`) REFERENCES `virtual_domains` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

4、数据库访问配置

在/etc/postfix配置目录创建sql配置目录。

sudo mkdir -p /etc/postfix/sql
cd /etc/postfix/sql

4.1、域名配置

创建文件:mysql-virtual-mailbox-domains.cf

user = user
password = password
hosts = 127.0.0.1
dbname = postfix
query = SELECT name FROM virtual_domains WHERE name=’%s’

4.2、邮箱配置

创建文件:mysql-virtual-mailbox-maps.cf

user = user
password = password
hosts = 127.0.0.1
dbname = postfix
query = SELECT 1 FROM virtual_users WHERE email=’%s’

4.3、用户配置

创建文件:mysql-email2email.cf

user = user
password = password
hosts = 127.0.0.1
dbname = postfix
query = SELECT email FROM virtual_users WHERE email=’%s’

4.3、邮箱别名配置

创建文件:mysql-virtual-alias-maps.cf

user = user
password = password
hosts = 127.0.0.1
dbname = postfix
query = SELECT destination FROM virtual_aliases WHERE source=’%s’

5、Sasl配置

创建文件:/etc/postfix/sasl/smtpd.conf

user = user
password = password
hosts = 127.0.0.1
dbname = postfix
sql_select: SELECT password FROM virtual_users WHERE email = ‘%u@%r’

6、修改/etc/postfix/main.cf

smtpd_sasl_path = smtpd

virtual_mailbox_domains = mysql:/etc/postfix/sql/mysql-virtual-mailbox-domains.cf
virtual_mailbox_maps = mysql:/etc/postfix/sql/mysql-virtual-mailbox-maps.cf
virtual_alias_maps = mysql:/etc/postfix/sql/mysql-email2email.cf,mysql:/etc/postfix/sql/mysql-virtual-alias-maps.cf

留下评论