Linux配置postfix邮件服务器
简介
Postfix 是一种 电子邮件服务器,是 MTA(邮件传输代理)软件,Dovecot 是一个开源的 IMAP 和 POP3 邮件服务器,POP / IMAP 是 MUA 从邮件服务器中读取邮件时使用的协议。
postfix的产生是为了替代传统的sendmail。相较于sendmail,postfix在速度、性能和稳定性上都更胜一筹。
1)Sendmail或Postfix:实现邮件的接收和中转,即SMTP;
2)Dovecot:实现邮件的收取,即POP3和IMAP;
3)Foxmail:客户端收发邮件的工具。
安装
1 2 3 4 5 6 7 8 9 10 11 12 13 | yum -y install postfix sendmail* m4 dovecot bind* alternatives --config mta //请按1 [root@test mail]# alternatives --config mta There are 2 programs which provide 'mta'. Selection Command ----------------------------------------------- + 1 /usr/sbin/sendmail.postfix * 2 /usr/sbin/sendmail.sendmail |
修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | [root@localhost ~]# vim /etc/postfix/main.cf 修改76:myhostname=mail.lhr.com //定义主机名 修改83:mydomain = lhr.com //定义域名 修改99:myorigin = $mydomain //从本机发出邮件的域名名称 修改116:inet_interfaces = all //监听的网卡接口 修改164:mydestination = $myhostname , $mydomain //设置可转发哪些主机的邮件 修改267: mynetworks = 0.0.0.0/0 # home_mailbox = Maildir/ #邮件目录,在用户的home下,默认位置/var/spool/mail/user or /var/mail/user [root@localhost ~]# vim /etc/dovecot/dovecot.conf 修改24:protocols = imap pop3 imtp //支持的电子邮件协议 修改48:login_trusted_networks = 0.0.0.0/0 //哪些人可以使用 listen = * ssl = no disable_plaintext_auth=no [root@localhost ~]#vim /etc/dovecot/conf.d/10-mail.conf 修改30:mail_location = mbox:~/mail:INBOX=/var/mail/%u echo '172.71.0.13 lhrdb mail.lhr.com lhr.com' >> /etc/hosts -- 检查配置文件是否有语法错误 postfix check |
创建邮件账号组及测试账号
1 2 3 4 5 | groupadd mailusers #添加邮件账号组 useradd -g mailusers -s /sbin/nologin lhr #创建测试账号lhr echo "lhr:lhr" | chpasswd useradd -g mailusers -s /sbin/nologin lhr1 echo "lhr1:lhr" | chpasswd |
重启服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | systemctl stop sendmail systemctl disable sendmail systemctl status sendmail systemctl restart postfix systemctl enable postfix systemctl status postfix systemctl restart dovecot systemctl enable dovecot systemctl status dovecot [root@test mail]# netstat -tulnp | grep 25 tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 409311/master tcp6 0 0 :::25 :::* LISTEN 409311/master [root@test mail]# netstat -tulnp | grep 110 tcp 0 0 0.0.0.0:110 0.0.0.0:* LISTEN 409548/dovecot tcp6 0 0 :::110 :::* LISTEN 409548/dovecot [root@test mail]# netstat -tulnp | grep dovecot tcp 0 0 0.0.0.0:110 0.0.0.0:* LISTEN 409548/dovecot tcp 0 0 0.0.0.0:143 0.0.0.0:* LISTEN 409548/dovecot tcp 0 0 0.0.0.0:993 0.0.0.0:* LISTEN 409548/dovecot tcp 0 0 0.0.0.0:995 0.0.0.0:* LISTEN 409548/dovecot tcp6 0 0 :::110 :::* LISTEN 409548/dovecot tcp6 0 0 :::143 :::* LISTEN 409548/dovecot tcp6 0 0 :::993 :::* LISTEN 409548/dovecot tcp6 0 0 :::995 :::* LISTEN 409548/dovecot |
测试发送邮件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | [root@test mail]# telnet mail.lhr.com 25 #连接mail服务器 Trying 172.71.0.13... Connected to mail.lhr.com. Escape character is '^]'. 220 mail.lhr.com ESMTP Postfix helo mail.lhr.com #声明本机的主机名 250 mail.lhr.com mail from:lhr@lhr.com #发件人 250 2.1.0 Ok rcpt to:lhr1@lhr.com #收件人 250 2.1.5 Ok data #开始编辑邮件的内容 354 End data with <CR><LF>.<CR><LF> subject: lhr test! # 主题 i am lhr . #以.结尾 250 2.0.0 Ok: queued as 2D4C2642D1C91 quit #退出 221 2.0.0 Bye Connection closed by foreign host. |
查看已发送的邮件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | [root@test mail]# cd /var/spool/mail/ [root@test mail]# ll total 4 -rw-rw---- 1 lhr mail 0 Feb 27 09:46 lhr -rw-rw---- 1 lhr1 mail 416 Feb 27 09:48 lhr1 -rw-rw---- 1 pure-ftp mail 0 Feb 14 11:51 pure-ftp -rw-rw---- 1 rpc mail 0 May 27 2022 rpc [root@test mail]# more lhr [root@test mail]# more lhr1 From lhr@lhr.com Mon Feb 27 09:48:55 2023 Return-Path: <lhr@lhr.com> X-Original-To: lhr1@lhr.com Delivered-To: lhr1@lhr.com Received: from mail.lhr.com (test [172.71.0.13]) by mail.lhr.com (Postfix) with SMTP id 2D4C2642D1C91 for <lhr1@lhr.com>; Mon, 27 Feb 2023 09:48:16 +0800 (CST) Message-Id: <20230227014828.2D4C2642D1C91@mail.lhr.com> Date: Mon, 27 Feb 2023 09:48:16 +0800 (CST) From: lhr@lhr.com i am lhr [root@test mail]# |
测试给QQ邮箱发送邮件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [root@lhrdb ~]# telnet mail.lhr.com 25 Trying 172.18.0.14... Connected to mail.lhr.com. Escape character is '^]'. 220 mail.lhr.com ESMTP Postfix helo mail.lhr.com 250 mail.lhr.com mail from:lhr@lhr.com 250 2.1.0 Ok rcpt to:lhrbest@qq.com 250 2.1.5 Ok data 4 End data with <CR><LF>.<CR><LF> subject: lhr test! i am lhr . 250 2.0.0 Ok: queued as ADCB460D2820 quit 221 2.0.0 Bye Connection closed by foreign host. |
QQ邮箱即可收到邮件:
如果发送失败,请查询:/var/spool/mail/lhr
文件,多半是SPF问题引起。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | [root@test mail]# more lhr From MAILER-DAEMON Mon Feb 27 10:40:52 2023 Return-Path: <> X-Original-To: lhr@lhr.com Delivered-To: lhr@lhr.com Received: by mail.lhr.com (Postfix) id D3A5E642D1C92; Mon, 27 Feb 2023 10:40:52 +0800 (CST) Date: Mon, 27 Feb 2023 10:40:52 +0800 (CST) From: MAILER-DAEMON@lhr.com (Mail Delivery System) Subject: Undelivered Mail Returned to Sender To: lhr@lhr.com Auto-Submitted: auto-replied MIME-Version: 1.0 Content-Type: multipart/report; report-type=delivery-status; boundary="A2C69642D1C91.1677465652/mail.lhr.com" Message-Id: <20230227024052.D3A5E642D1C92@mail.lhr.com> This is a MIME-encapsulated message. --A2C69642D1C91.1677465652/mail.lhr.com Content-Description: Notification Content-Type: text/plain; charset=us-ascii This is the mail system at host mail.lhr.com. I'm sorry to have to inform you that your message could not be delivered to one or more recipients. It's attached below. For further assistance, please send mail to postmaster. If you do so, please include this problem report. You can delete your own text from the attached returned message. The mail system <lhrbest@qq.com>: host mx3.qq.com[113.96.208.206] said: 550 SPF check failed [MIM6ntSUV3P03F9oBHWRg0dB/ai6E+wgHOwPffsr67eYK3Rw1W5xcT2LZw7zD6jolA== IP: 202.101.190.134]. https://service.mail.qq.com/detail/122/72. (in reply to end of DATA command) --A2C69642D1C91.1677465652/mail.lhr.com Content-Description: Delivery report Content-Type: message/delivery-status Reporting-MTA: dns; mail.lhr.com X-Postfix-Queue-ID: A2C69642D1C91 X-Postfix-Sender: rfc822; lhr@lhr.com Arrival-Date: Mon, 27 Feb 2023 10:40:22 +0800 (CST) Final-Recipient: rfc822; lhrbest@qq.com Original-Recipient: rfc822;lhrbest@qq.com Action: failed Status: 5.0.0 Remote-MTA: dns; mx3.qq.com Diagnostic-Code: smtp; 550 SPF check failed [MIM6ntSUV3P03F9oBHWRg0dB/ai6E+wgHOwPffsr67eYK3Rw1W5xcT2LZw7zD6jolA== IP: 202.101.190.134]. https://service.mail.qq.com/detail/122/72. --A2C69642D1C91.1677465652/mail.lhr.com Content-Description: Undelivered Message Content-Type: message/rfc822 Return-Path: <lhr@lhr.com> Received: from mail.lhr.com (test [172.71.0.13]) by mail.lhr.com (Postfix) with SMTP id A2C69642D1C91 for <lhrbest@qq.com>; Mon, 27 Feb 2023 10:40:22 +0800 (CST) subject: lhr test! Message-Id: <20230227024026.A2C69642D1C91@mail.lhr.com> Date: Mon, 27 Feb 2023 10:40:22 +0800 (CST) From: lhr@lhr.com i am lhr --A2C69642D1C91.1677465652/mail.lhr.com-- [root@test mail]# pwd /var/spool/mail |
什么是SPF?
(Sender Policy Framework) 的缩写,一种以IP地址认证电子邮件发件人身份的技术,是非常高效的垃圾邮件解决方案。
接收邮件方会首先检查域名的SPF记录,来确定发件人的IP地址是否被包含在SPF记录里面,如果在,就认为是一封正确的邮件,否则会认为是一封伪造的邮件进行退回。