使用Postfix和Dovecot搭建邮件收发服务器
第一步:准备一台服务器,一台客户机。服务器搭建域名服务器或者使用其他域名服务器,能够将域名hbp.com的mail记录和MX记录解析到服务器,这里设置服务器的IP地址为192.168.182.139,客户机的IP地址为192.168.182.128,并且在服务器安装named服务,设置解析记录,解析记录如下:
[root@mail ~]# cat /var/named/hbp.com.zone $TTL 1D @ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 192.168.182.139 @ MX 10 mail.hbp.com. mail A 192.168.182.139
2.第二步:在服务器端安装postfix软件包,并设置为开机自启动
yum -y install postfix;chkconfig postfix on
进行相关配置,主要配置文件如下作为参考:
配置文件目录:/etc/postfix 服务配置文件:/etc/postfix/main.cf 主程序配置:/etc/postfix/master.cf 管理程序配置目录:/usr/sbin/ postalias postmap postconf postfix postqueue postsuper 邮件通信日志:/var/log/maillog 邮件队列目录:/var/spool/postfix/
配置命令如下:
查看postfix服务配置 postconf -n 查看非默认配置 postconf -d 查看默认配置 修改配置 postconf -e ‘参数1=值1,值2’……
在配置过程中可以使用postconf命令提取出有效的配置方便我们进行修改,修改后的主要配置如下:
[root@mail ~]# cat /etc/postfix/main.cf alias_database = hash:/etc/aliases alias_maps = hash:/etc/aliases command_directory = /usr/sbin config_directory = /etc/postfix daemon_directory = /usr/libexec/postfix data_directory = /var/lib/postfix debug_peer_level = 2 html_directory = no inet_interfaces = all //网络接口地址 inet_protocols = all mail_owner = postfix mailq_path = /usr/bin/mailq.postfix manpage_directory = /usr/share/man myhostname=mail.hbp.com //主机名 mydomain=hbp.com//主要邮件域 myorigin=$mydomain//默认的发件域 mydestination = $myhostname, $mydomain,localhost //本地管辖的投递域 home_mailbox=Maildir/ //指定邮箱类型为maildir,每个用户一个目录 newaliases_path = /usr/bin/newaliases.postfix queue_directory = /var/spool/postfix readme_directory = /usr/share/doc/postfix-2.6.6/README_FILES sample_directory = /usr/share/doc/postfix-2.6.6/samples sendmail_path = /usr/sbin/sendmail.postfix setgid_group = postdrop unknown_local_recipient_reject_code = 550
第三步,重启服务,确定监听状态,发送邮件测试。
[root@mail ~]# service postfix restart 关闭 postfix: [确定] 启动 postfix: [确定] [root@mail ~]# netstat -anpt |grep :25 tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 29531/master tcp 0 0 :::25 :::* LISTEN 29531/master [root@mail ~]# echo "Hello,postfix" | mail -s "zhuti" -r fanfan@hbp.com fanxiaomo@hbp.com [root@mail ~]# mail -f ~fanxiaomo/Maildir/ Heirloom Mail version 12.4 7/29/08. Type ? for help. "/home/fanxiaomo/Maildir/": 7 messages 1 unread 1 root Wed Jul 26 11:25 17/497 "123" 2 Mail Delivery System Wed Jul 26 14:08 78/2636 "Undelivered Mail Returned to Sender" 3 Mail Delivery System Wed Jul 26 14:09 77/2635 "Undelivered Mail Returned to Sender" 4 fanfan@hbp.com Wed Jul 26 14:14 17/503 "Test Mail 1" 5 fanxiaomo@hbp.com Wed Jul 26 14:36 18/577 "sayhello" 6 fanfan Wed Jul 26 14:54 22/803 "Re: 你好哦" >U 7 fanfan@hbp.com Wed Jul 26 15:23 17/501 "zhuti"
发送邮件的命令格式为:
echo “邮件内容” | mail -s "主题" -r 发件人 收件人1,收件人2,……
查看邮件使用命令:mail -f 邮件目录,可以看到第七封邮件使我们刚刚发送的邮件,
& 7 Message 7: From fanfan@hbp.com Wed Jul 26 15:23:12 2017 Return-Path: <fanfan@hbp.com> X-Original-To: fanxiaomo@hbp.com Delivered-To: fanxiaomo@hbp.com Date: Wed, 26 Jul 2017 15:23:12 +0800 From: fanfan@hbp.com To: fanxiaomo@hbp.com Subject: zhuti User-Agent: Heirloom mailx 12.4 7/29/08 Content-Type: text/plain; charset=us-ascii Status: RO Hello,postfix
第四步:在客户机安装Dovecot并进行配置,主要修改dovecot服务配置,允许明文认证,修改服务的邮箱配置,匹配到用户的邮箱目录
yum -y install dovecot chkconfig dovecot on vim /etc/dovecot/conf.d/10-auth.conf disable_plaintext_auth=no //允许明文认证 vim /etc/dovecot/conf.d/10-mail.conf mail_location=maildir:~/Maildir //指定邮箱类型以及路径 service dovecot restart
第五步:在客户机上为mail工具建立配置文件,添加如下配置
vim /etc/mail.rc set bsdcompat set smtp=smtp://mail.hbp.com set from=fanxiaomo@hbp.com set folder=imap://fanxiaomo@mail.hbp.com
在客户机上测试收取邮件,输入mail命令,则会提示输入密码,输入fanxiaomo@hbp.com对应的密码就能收取邮件了,
[root@localhost ~]# mail Password: Heirloom Mail version 12.4 7/29/08. Type ? for help. "imap://fanxiaomo@mail.hbp.com/INBOX": 7 messages > 1 root Wed Jul 26 11:25 /512 "123" 2 Mail Delivery System Wed Jul 26 14:08 /2712 "Undelivered Mail Returned to Sender" 3 Mail Delivery System Wed Jul 26 14:09 /2710 "Undelivered Mail Returned to Sender" 4 fanfan@hbp.com Wed Jul 26 14:14 /518 "Test Mail 1" 5 To fanxiaomo@hbp.com Wed Jul 26 14:36 /593 "sayhello" 6 fanfan Wed Jul 26 14:54 /825 "Re: 你好哦" 7 fanfan@hbp.com Wed Jul 26 15:23 /516 "zhuti" &
第六步:在客户机上使用第三方客户端来收发邮件进行测试,这里我们选择使用Thunderbird来进行测试,打开软件添加账号,软件会自动进行匹配邮件服务器,
如下图:
点击完成之后,由于没有进行SSL加密连接,这里就确认安全例外就行了,一次添加多个账号,来进行邮件收发测试,结果如下图,收发邮件可以正常进行。
到这里邮件服务器的收发已经完全可以进行了,但是没有进行SMTP认证,所有人都可以无限制的发送邮件,包括垃圾邮件,所以我们需要开启SMTP认证。
第七步:在服务器端开启SMTP认证。安装saslauthd服务,启动服务并进行测试:
yum install cyrus-sasl-plain -y service saslauthd start chkconfig saslauthd on [root@mail ~]# testsaslauthd -u fanfan -p 123456 -s smtp 0: OK "Success." //信息正确,通过验证 [root@mail ~]# testsaslauthd -u fanfan -p 1234567 -s smtp 0: NO "authentication failed" //信息错误,验证失败 [root@mail ~]#
接下来修改postfix的配置文件,开启SMTP认证,添加以下配置文件
mynetworks=127.0.0.1 smtpd_sasl_auth_enable=yes smtpd_sasl_security_options=noanonymous smtpd_recipient_restrictions= permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
未重启postfix服务,SMTP服务未生效之前,在客户机进行测试,结果如下:
smtp-server: 554 5.7.1 <fanxiaomo@126.com>: Relay access denied "/root/dead.letter" 11/309 . . . message not sent. ^C [root@localhost ~]#
重启postfix服务,在客户机mail配置中添加SMTP认证信息:
set bsdcompat set smtp=smtp://mail.hbp.com set from=fanxiaomo@hbp.com set folder=imap://fanxiaomo@mail.hbp.com set smtp-auth-user="fanxiaomo@hbp.com" set smtp-auth-password="123456" set password-fanxiaomo@mail.hbp.com="123456"
再次测试,邮件成功发送。
[root@mail ~]# echo "SMTP Test." | mail -s "Test Mail 3." fanxiaomo@126.com [root@mail ~]#
原文链接:使用Postfix和Dovecot搭建邮件服务器,转载请注明来源!