服务器操作系统:Linux (centos 6.2)
postfix 、dovecot 版本:直接使用 yum 安装即可。
postfix 根目录:/etc/postfix
dovecot 根目录:/etc/dovecot
假设你的邮箱使用域名为:mailtest.cn 需要对该域名进行解析操作(增加 mx 解析记录)
假设服务器IP:123.123.123.123(备注:外网IP)
需要开放的端口:25、110、143
mysql数据库相关信息
数据库host:127.0.0.1(根据msql所在服务器实际IP填写,此处只是举例)
数据库端口: 3306 (必须是 3306)
数据库名称:mailserver
数据库用户: root
数据库密码: 123456
解析操作:为域名mailtest.cn 新增一条 mx 解析记录,并且该记录指向IP:123.123.123.123
postfix 仅提供 smtp 服务,不提供 pop3 和 imap 服务,主要是用发送和接收邮件的(接收到的邮件后,一般转交 dovecot 处理,dovecot 负责将 postfix 转发过来的邮件保存到服务器硬盘上)
dovecot 仅提供 pop3 和 imap 服务,不提供 smtp 服务(Foxmail之类的邮箱客户端,都是通过pop3 和 imap 来收发邮件的。发邮件时,dovecot 会将邮件转交给 postfix 来发送)
综上,postfix 用来发邮件,dovecot 用来收邮件
( 重要!) postfix发送邮件的日志文件:/var/log/maillog
1.1 修改主机名:
[root@a ~] vi /etc/sysconfig/network
修改 HOSTNAME=mx.mailtest.cn 并保存,然后重启服务器。如果没有重启服务器,更改主机名不会生效
1.2检测主机名
[root@root ~] hostname
mail.devstore.cn
1.3开放防火墙中的 25、110、143 端口
[root@root ~] /sbin/iptables -I INPUT -p tcp --dport 25-j ACCEPT
[root@root ~] /sbin/iptables -I INPUT -p tcp --dport 110-j ACCEPT
[root@root ~] /sbin/iptables -I INPUT -p tcp --dport 143-j ACCEPT
[root@root ~] service iptables save
[root@root ~] service iptables restart
2.1查看SELinux状态
[root@root ~] /usr/sbin/sestatus -v
( 查看SELinux是否开启。如果不是显示: SELinux status: disabled 则代表着SELinux处于开启状态 )
2.2关闭SELinux的操作:
[root@root ~] vi /etc/selinux/config
将 SELINUX=enforcing 改为 SELINUX=disabled ,然后重启机器(必须重启,修改才能生效)。重启后再查看一次状态,看看SELinux是否已经关闭
centos 6.2 默认已经安装了 postfix ( postfix根目录:/etc/postfix)
3.1 postfix状态查看
[root@root ~] service postfix status
master (pid 4366) 正在运行...
3.2查看系统 MTA
[root@root ~] alternatives --display mta (查看系统 MTA:邮件传送端,即常说的邮件服务器是否为postfix)
mta - 状态是自动。
链接目前指向/usr/sbin/sendmail.postfix
/usr/sbin/sendmail.postfix - 优先度 30
从 mta-mailq:/usr/bin/mailq.postfix
从 mta-newaliases:/usr/bin/newaliases.postfix
从 mta-pam:/etc/pam.d/smtp.postfix
从 mta-rmail:/usr/bin/rmail.postfix
从 mta-sendmail:/usr/lib/sendmail.postfix
从 mta-mailqman:/usr/share/man/man1/mailq.postfix.1.gz
从mta-newaliasesman:/usr/share/man/man1/newaliases.postfix.1.gz
从 mta-sendmailman:/usr/share/man/man1/sendmail.postfix.1.gz
从 mta-aliasesman:/usr/share/man/man5/aliases.postfix.5.gz
当前"最佳"版本是/usr/sbin/sendmail.postfix。
(sendmail.postfix说明当前 MTA的确就是 postfix)
[root@root ~] yum -y install postfix* dovecot*
创建 vmail 用户
[root@root ~] groupadd -g 2000 vmail ( 指定新建用户组vmail的ID为2000 )
[root@root ~] useradd -g vmail -u 2000 vmail -d/var/vmail (指定新建用户vmail的ID为2000,用户根目录为/var/vmail )
[root@root ~] chown -R vmail:dovecot /etc/dovecot
[root@root ~] chmod -R o-rwx /etc/dovecot
6.1mailserver数据库需要新建1张表。表结构参照如下:
CREATE TABLE `tb_user_email` (
`id` bigint(20)NOT NULL AUTO_INCREMENT,
`user_id`bigint(20) NOT NULL,
`username`varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '用户邮箱名',
`domain`varchar(255) CHARACTER SET utf8 NOT NULL COMMENT '用户的邮箱域名',
`password`varchar(255) CHARACTER SET utf8 NOT NULL,
`home`varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`uid` int(11)DEFAULT NULL,
`gid` int(11)DEFAULT NULL,
`active` char(1)CHARACTER SET utf8 NOT NULL DEFAULT 'Y',
`create_time`datetime NOT NULL COMMENT '创建时间',
`create_user_id`bigint(20) DEFAULT NULL COMMENT '创建用户id',
`last_update_time` datetime DEFAULT NULL COMMENT '最后一次修改时间',
`last_update_user_id` bigint(20) DEFAULT NULL COMMENT '最后一次修改的用户id',
`curr_type`int(5) NOT NULL DEFAULT '1' COMMENT '当前状态',
`source`varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`destination`varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`type` int(5) NOTNULL COMMENT '邮箱类型 1iClap个人邮箱 2私有域名邮箱',
PRIMARY KEY(`id`),
KEY `user_id`(`user_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=632 DEFAULT CHARSET=utf8COLLATE=utf8_unicode_ci;
6.2表结构需要额外说明的列:
active: 账号是否“激活”状态,默认为Y
domain:postfix会检查接收到的邮件域名是否合法,及@后面的字符串
6.3关于表中密码列 password 的值的说明
e10adc3949ba59abbe56e057f20f883e 是 123456 的MD5加密后的值
25f9e794323b453885f5181f1b624d0b 是 123456789 的MD5加密后的值
7.1新建 /etc/postfix/mysql-virtual-mailbox-domains.cf 文件
hosts = 127.0.0.1
user = root
password = 123456
dbname = mailserver
query = SELECT 1 FROM tb_user_email WHERE domain='%s'
# %s代表:域名
7.2新建 /etc/postfix/mysql-virtual-mailbox-maps.cf 文件
hosts = 127.0.0.1
user = root
password = 123456
dbname = mailserver
query = SELECT 1 FROM tb_user_email WHERE username='%u'
# %u 代表:用户账号
7.3新建 /etc/postfix/mysql-virtual-alias-maps.cf 文件
hosts = 42.51.172.27
user = root
password = devdb
dbname = dev_store
query = SELECT destination FROMuser_enterprise_email WHERE source='%s'
# %s 代表:域名
后续会有测试命令可以测试以上配置是否正确
1修改/etc/postfix/main.cf 文件(注意,以下配置内容中,等于号 = 的左右 都必须有 空格):
[root@root ~] cd /etc/postfix/
[root@root postfix] vi main.cf
myhostname = mail.devstore.cn #Mail服务器域名,EHLO名称。邮箱名称 ( xxx@mail.devstore.cn ) (主机名hostname的值必须和这个相同,否则会报错的)
mydomain = devstore.cn #邮箱域名 (xxx@devstore.cn )。必要的时候,你也可以设置成 mydomain =mail.devstore.cn ( xxx@mail.devstore.cn )
myorigin = $mydomain
inet_interfaces = all #如果 inet_interfaces= localhost,则邮箱不接收外网的邮件,只接受内网的邮件。inet_interfaces= all 则邮箱会接收外网的邮件
mydestination = localhost #本地邮件域名,直接接收
mynetworks = 127.0.0.0/8 #116.255.217.0/28,127.0.0.0/8 #允许转发的来源IP,该值设置后,mynetworks_style参数将被忽略掉
relay_domains = $mydestination
virtual_transport = lmtp:unix:private/dovecot-lmtp (这个配置非常重要,是psotfix将接收到的邮件转交给 dovecot处理的关键配置 )
virtual_mailbox_maps =mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf (下面这3个文件就是调用刚才新创建的那3个文件来连接mysql数据库的 )
virtual_alias_maps =mysql:/etc/postfix/mysql-virtual-alias-maps.cf
virtual_mailbox_domains =mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated, reject_unauth_destination, permit
broken_sasl_auth_clients = yes
#限制每一封邮件最大容量为 20M
#message_size_limit = 20480000
#限制postfix最大并发连接数
#default_process_limit = 100
修改好文件后,保存并退出。然后重启psotfix:service postfixrestart
配置 dovecot 需要修改以下 8 个文件 ( dovecot根目录:/etc/dovecot ):
/etc/dovecot/dovecot.conf Dovecot的主配置文件
/etc/dovecot/conf.d/10-auth.conf 用户验证相关配置信息
/etc/dovecot/conf.d/10-mail.conf Dovecot将要操作的磁盘路径相关配置信息
/etc/dovecot/conf.d/10-master.conf Dovecot本地socket相关配置信息
/etc/dovecot/conf.d/10-ssl.conf 关于SSL的相关配置信息
/etc/dovecot/conf.d/20-pop3.conf 关于POP3的相关配置信息
/etc/dovecot/conf.d/auth-sql.conf.ext SQL-Type验证相关配置信息
/etc/dovecot/dovecot-sql.conf.ext Dovecot与数据库连接相关配置信息。example文件位置:/usr/share/doc/dovecot-2.0.9/example-config/
1 [root@root dovecot] vi/etc/dovecot/dovecot.conf
[root@root dovecot] vi /etc/dovecot/dovecot.conf
protocols = imap pop3 lmtp
2 [root@root dovecot] vi /etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth = no
auth_mechanisms = plain login
#禁止系统用户登录 (在 !include auth-system.conf.ext前面加个 #号 )
#!include auth-system.conf.ext
#启用mysql中的用户 (把 #!include auth-sql.conf.ext前面的 #号删除掉 )
!include auth-sql.conf.ext
3 [root@root dovecot] vi/etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:/var/vmail/%d/%n
mail_privileged_group = vmail
# namespace inbox 会因为dovecot的版本不同而有所改变
# Error: user xxx@mail.devstore.cn: Initializationfailed: namespace configuration error: inbox=yes namespace missing
# 如果系统日志/var/log/maillog中报上面的错误,则加入以下内容
namespace inbox {
inbox = yes
}
4 [root@root dovecot] vi/etc/dovecot/conf.d/10-master.conf
#-------------------------------------------------------------可选操作 begin-----------------------------------------------------------------------------------
#通过将端口设置为0,以禁用非SSL加密的IMAP和POP3协议
service imap-login {
inet_listener imap {
port = 0
}
...
}
service pop3-login {
inet_listener pop3 {
port = 0
}
...
}
#-------------------------------------------------------------可选操作 end-----------------------------------------------------------------------------------
# 这个配置非常重要,是用来接收psotfix转交过来的邮件的关键配置
service lmtp {
unix_listener /var/spool/postfix/private/dovecot-lmtp {
mode = 0600
user = postfix
group = postfix
}
#inet_listener lmtp {
#Avoid making LMTP visible for the entire internet
#address =
#port =
#}
service auth {
unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix
}
unix_listener auth-userdb {
mode = 0600
user = vmail
#group =
}
# Auth process is run as this user.
user = dovecot
}
service auth-worker {
user = vmail
}
5 [root@root dovecot] vi/etc/dovecot/conf.d/10-ssl.conf
ssl = no
6 [root@root dovecot] vi /etc/dovecot/conf.d/20-pop3.conf
pop3_uidl_format = %08Xu%08Xv
pop3_client_workarounds = outlook-no-nuls oe-ns-eoh
7 [root@root dovecot] vi/etc/dovecot/conf.d/auth-sql.conf.ext
passdb {
driver = sql
args = /etc/dovecot/dovecot-sql.conf.ext
}
#userdb {
# driver = sql
# args = /etc/dovecot/dovecot-sql.conf.ext
#}
userdb {
driver = static
args = uid=vmail gid=vmail home=/var/vmail/%d/%n
#注意这里的 home需要和 mail_location配置的值相同 ( mail_location在10-mail.conf文件中 )
}
8 [root@root dovecot] vi /etc/dovecot/dovecot-sql.conf.ext
(dovecot-sql.conf.ext文件所在位置:/usr/share/doc/dovecot-2.0.9/example-config/dovecot-sql.conf.ext,请先下载此文件后,再执行编辑vi命令)
driver = mysql
connect = host=172.10.2.162 dbname=mailserver user=rootpassword=123456
default_pass_scheme = MD5 # (default_pass_scheme是指用户的密码的加密方式:对应users表中password列的值 )
#default_pass_scheme = PLAIN #明文,不加密
#PLAIN: Password is in plaintext.
#CRYPT: Traditional DES-crypted password in /etc/passwd(e.g. "pass" = vpvKh.SaNbR6s)
#Dovecot uses libc's crypt() function, which means thatCRYPT is usually able to recognize MD5-CRYPT and possibly also other passwordschemes. Please see the notes below regarding glibc's crypt() and SHA-256/512support.
#The traditional DES-crypt scheme only uses the first 8characters of the password, the rest are ignored. Other schemes may have otherpassword length limitations (if they limit the password length at all).
#MD5-CRYPT: MD5 based salted password hash nowadayscommonly used in /etc/shadow. (e.g. "pass" =$1$ozdpg0V0$0fb643pVsPtHVPX8mCZYW/)
#MD5: Alias for MD5-CRYPT. Dovecot versions earlier thanv1.0.rc16 need to use this instead of MD5-CRYPT. This name is deprecatedbecause MD5-CRYPT isn't an actual MD5 hash.
#PLAIN-MD5: An actual MD5 hash of the password. (e.g."pass" = 1a1dc91c907325c69271ddf0c944bc72)
上面 8个文件修改完成后:
重启 dovecot
[root@root postfix] service dovecot restart
重启 postfix
[root@root dovecot] service postfix restart
到此为止,postfix和dovecot都配置好了,并且启用了 smtp、pop3、imap 服务
[root@root dovecot] cat /var/log/maillog 查看日志.
联系客服