技术小阿哥 2017-11-28 2173浏览量
说到邮件系统,现在有很多,比如微软的Exchange、Office365、IBM Lotus、主流的第三方提供的邮件系统。当然这些都是收费的,如果对于小环境内使用的话,可以使用linux下部署的Postfix+Dovecot实现邮件的收发等,当然在Linux下 有很多服务可以实现邮件的收发,今天我们主要介绍的是Centos7+Postfix+Dovecot实现邮件收发,具体见下:
首先是安装好Centos7,安装好后,我们需要对一些系统的设置进行配置;
1
2
|
1.selinux关闭、 sed –I ‘s /enforcing/disabled/g ’ /etc/selinux/config
2. firewall-cmd –add-port=’110 /tcp ’ –permanent firewall-cmd –add-port=’25 /tcp ’ –permanent
|
准备好后,首先是查看系统版本
1
|
cat /etc/redhat-release
|
Centos7是自带postfix服务的,所以我们不用单独安装了;
1
|
rpm –qa | grep postfix
|
我们发现系统自带了postfix服务,所以我们就不用安装了
postfix 2.10.1-6.el7.x86_64
接下来就是说说postfix的基本配置,当然postfix的配置非常多,我们就不多介绍了,先介绍一下发送邮件的基本配置
我们先通过取反查看默认的postfix的默认配置,查看那些需要修改
1
|
grep - v '^#' /etc/postfix/main .cf | grep - v '^$'
|
1
|
vim /etc/postfix/main .cf
|
1
2
3
|
myhostname =pfx.ixmsoft.com #定义主机名
mydomain =ixmsoft.com #定义域名
myorigin =$mydomain #定义组织域
|
1
2
|
inet_interfaces =all 定义网络区域 inet_portocols =all 定义协议 |
1
2
|
我们需要在mydestination的值后面添加 $mydomain ,如果不添加的话,收件人的地址会有问题,如果添加后,用户发送邮件直接会是 xx@domain.com |
1
|
取消注释 #local_recipient_maps =
|
1
|
grep - v '^#' /etc/postfix/main .cf | grep - v '^$'
|
修改保存退出后,我们查看反向配置
保存退出后,我们启动postfix服务及查看端口状态,
1
2
3
|
systemctl start postfix system enable postfix
netstat –anlpt
|
接下来我们安装dovecot服务,该服务主要提供pop3、imap服务
1
|
yum install dovecot
|
安装完成
接下来我们查看dovecot的默认配置文件
我们看见以下include可以包含的配置文件路劲,在该路劲下的*.conf文件都会生效
我们启用协议以下协议
1
2
3
|
protocols = imap pop3 lmtp 启用端口监听 listen = *,:: |
保存退出
1
|
grep - v '^#' /etc/dovecot/dovecot .conf | grep - v '^$'
|
1
2
|
vim /etc/dovecot/conf .d /10-auth .conf
disable_plaintext_auth = no |
1
2
|
auth_mechanisms = plain login !include auth-system.conf.ext |
保存退出
1
|
grep - v '^#' /etc/dovecot/conf .d /10-auth .conf | grep - v '^$'
|
禁用ssl认证,如下:
当然也可以不取消ssl,根据自己的环境定
1
2
|
vim /etc/dovecot/conf .d /10-ssl .conf
ssl = no |
启用dovecot的日志,如下:
1
2
3
4
|
vim /etc/dovecot/conf .d /10-logging .conf
添加 info_log_path = /var/log/dovecot_info .log
debug_log_path = /var/log/dovecot_debug .log
|
然后启动服务
1
2
|
systemctl start dovecot systemctl enable dovecot
|
我们看见110、143都已经监听了
接下来我们创建一个测试用户
1
2
|
useradd zs
passwd zs
|
然后输入新旧密码
然后我们开始测试
1
2
3
4
5
6
7
8
9
10
|
[root@pfx ~] # telnet localhost 110
Trying ::1... Connected to localhost. Escape character is '^]' .
+OK Dovecot ready. user zs +OK pass 123456 -ERR [SYS /TEMP ] Internal error occurred. Refer to server log for more information.
Connection closed by foreign host. |
我们查看log
1
2
3
4
5
6
7
8
9
10
11
|
[root@pfx ~] # tail -f /var/log/maillog
Dec 21 00:37:58 pfx postfix /postfix-script [9570]: stopping the Postfix mail system
Dec 21 00:37:58 pfx postfix /master [9553]: terminating on signal 15
Dec 21 00:38:01 pfx postfix /postfix-script [9654]: starting the Postfix mail system
Dec 21 00:38:01 pfx postfix /master [9656]: daemon started -- version 2.10.1, configuration /etc/postfix
Dec 21 00:38:08 pfx postfix /smtpd [9661]: warning: hostname bogon does not resolve to address 192.168.5.20: Name or service not known
Dec 21 00:38:08 pfx postfix /smtpd [9661]: connect from unknown[192.168.5.20]
Dec 21 00:38:20 pfx postfix /smtpd [9661]: lost connection after CONNECT from unknown[192.168.5.20]
Dec 21 00:38:20 pfx postfix /smtpd [9661]: disconnect from unknown[192.168.5.20]
Dec 21 00:55:46 pfx dovecot: pop3(zs): Error: user zs: Initialization failed: Namespace '' : Mail storage autodetection failed with home= /home/zs
Dec 21 00:55:46 pfx dovecot: pop3(zs): Error: Invalid user settings. Refer to server log for more information.
|
我们切换到刚才的用户
1
2
3
|
su – zs
然后为用户zs,创建inbox目录 mkdir -p ~ /mail/ .imap /INBOX
|
然后再次尝试就可以了
1
2
3
4
5
6
7
8
9
|
[root@pfx ~] # telnet localhost 110
Trying ::1... Connected to localhost. Escape character is '^]' .
+OK Dovecot ready. user zs +OK pass 123456 +OK Logged in .
|
测试发送邮件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@pfx /] # telnet localhost 25
Trying ::1... Connected to localhost. Escape character is '^]' .
220 mail.ixmsoft.com ESMTP Postfix mail from:zs@ixmsoft.com 250 2.1.0 Ok rcpt to: ls @ixmsoft.com
250 2.1.5 Ok data 354 End data with <CR><LF>.<CR><LF> this is test mail
. 250 2.0.0 Ok: queued as A702D400FC14 quit 221 2.0.0 Bye Connection closed by foreign host. |
我们查看邮件
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@pfx /] # telnet localhost 110
Trying ::1... Connected to localhost. Escape character is '^]' .
+OK Dovecot ready. user ls
+OK pass 123456 +OK Logged in .
list +OK 1 messages: 1 409 . |
我们每次都需要创建目录,比较麻烦,我们可以不可以用脚本呢
设置邮件存放目录:
1
2
|
vim /etc/dovecot/conf .d /10-mail .conf 取消以下注释
mail_location = mbox:~ /mail :INBOX= /var/mail/ %u #指定邮件的位置
|
1
2
3
4
5
6
7
8
9
10
|
重启dovecot服务 systemctl restart dovecot 修改创建用户模板文件,使用户创建时自动生成mail存放目录 vim /etc/skel/ .bash_profile 添加一下配置文件,
if [ ! -d ~ /mail/ .imap /INBOX ] ; then
mkdir -p ~ /mail/ .imap /INBOX
fi 注:如果是复制的,最好在保存推出前,执行以下编码格式的执行 : set format =unix
或者可以下载附件,替换当前的配置文件即可 |
保存退出后,执行一下,为其快速生效。
1
|
source .bash_profile
|
接下来我们需要创建测试用户,创建用户后,使用新用户登录后,会自动创建相关目录,然后就直接可以使用邮箱了;我们也可以使用outlook或者其他方式进行邮件测试,再次就不做测试;
如果想希望对外及对内邮件有来往的话,需要将服务器的110、25发布到外网,然后再外网的域名商配置MX记录解析及A记录解析,这样邮件才能回来。
我们最后使用foxmail进行内网测试了;
我们配置两个账户,zs、ls用户进行邮件收发测试:
我们再添加一个ls的账户
使用zs给ls发邮件
zs收到邮件了
ls也收到邮件了
本文转自 高文龙 51CTO博客,原文链接:http://blog.51cto.com/gaowenlong/1884841,如需转载请自行联系原作者
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
云安全开发者的大本营