整體參考:https://www.cnblogs.com/chris-cp/p/4843407.html
建議先看上文,照着配置完再看本文。關於25端口被封無法發郵件的解決辦法在本文后半部分。
做幾點補充:
/etc/dovecot/conf.d/10-ssl.conf
中把ssl = required
改成ssl = no
- 注意設置
/etc/postfix/main.cf
中的mynetworks
時,注意本地子網的IP格式,比如172.21.0.1/28就是錯的,要寫成172.21.0.0/28。網段計算器
安全組開放25,110,143端口
常用命令:
查看郵件隊列
postqueue -p # 另一個相似命令:mailq
查看日志:
systemctl status postfix
journalctl -u postfix
# 下面這個查看的日志詳細
journalctl -u postfix@-
如果你只圖能收郵件,不需要往外發郵件的話,到這里已經可以結束了。
解決雲服務器封了25端口導致的不能發送郵件的問題
網上說開放465端口,用SSL,我照着postfix+dovecot+SSL 搭建郵件服務器做了,但是發送郵件一直還是用25端口。
最后看了半天官方的文檔,升級到了postfix3,按照官方給的說明搗鼓了半天終於是解決了。接下來是正文。
本文提供兩種方法,一種是設置transport_maps,對特定地址的郵件指定端口,另一種是用iptables把25端口NAT到465端口。第一種方法測試后不太現實,推薦第二種方法。
1. 安裝postfix3
systemctl stop postfix
yum remove postfix
yum install https://mirror.ghettoforge.org/distributions/gf/el/7/plus/x86_64/postfix3-3.6.2-1.gf.el7.x86_64.rpm
注意,我是Centos 7,如果你是Centos8,請使用如下命令:
yum install https://mirror.ghettoforge.org/distributions/gf/el/8/plus/x86_64/postfix3-3.6.2-1.gf.el8.x86_64.rpm
如果有版本更新,上面鏈接失效了,自己去網站看下最新版鏈接。
2. 配置postfix client tls
這里科普一點小知識,postfix分為client和server,client是往外發郵件的,server是接收郵件的。我們發郵件要用465,所以設置的是client,配置項以
smtp_
開頭。smtpd_
是服務器配置。
先跟着上文中提到的postfix+dovecot+SSL 搭建郵件服務器生成SSL證書和key,並配置好smtp-server端。
上述操作后部分配置如下:
# /etc/dovecot/conf.d/10-ssl.conf
ssl = yes
ssl_cert = </etc/pki/tls/certs/server.crt
ssl_key = </etc/pki/tls/certs/server.key
# /etc/postfix/main.cf
smtpd_use_tls = yes
smtpd_tls_cert_file = /etc/pki/tls/certs/server.crt
smtpd_tls_key_file = /etc/pki/tls/certs/server.key
smtpd_tls_session_cache_database = btree:/etc/postfix/smtpd_scache
到這里,在郵件客戶端開啟SSL收件應該已經不成問題,但是還無法發郵件。
然后參看官網的指導:
http://www.postfix.org/TLS_README.html#quick-client
http://www.postfix.org/TLS_README.html#client_smtps
先來自動生成tls client端的配置項。只有tls配置是默認時此命令才有效,否則不會更改配置文件。
postfix tls enable-client
執行結束后/etc/postfix/main.cf
中應當添加了
smtp_tls_session_cache_database = btree:/var/lib/postfix/smtp_scache
smtp_tls_security_level = may
tls_random_source = dev:/dev/urandom
smtp_tls_loglevel = 1
3. 方法一、設置transport_maps 走465端口
根據官網的說明:
Postfix ≥ 3.0: Sending all remote mail to an SMTPS server
The first example will send all remote mail over SMTPS through a provider's server called "mail.example.com":
/etc/postfix/main.cf: # Client-side SMTPS requires "encrypt" or stronger. smtp_tls_security_level = encrypt smtp_tls_wrappermode = yes # The [] suppress MX lookups. relayhost = [mail.example.com]:465
Use
postfix reload
to make the change effective.
See SOHO_README for additional information about SASL authentication.
Postfix ≥ 3.0: Sending only mail for a specific destination via SMTPS
The second example will send only mail for "example.com" via SMTPS. This time, Postfix uses a transport map to deliver only mail for "example.com" via SMTPS:
/etc/postfix/main.cf: transport_maps = hash:/etc/postfix/transport
/etc/postfix/transport: example.com relay-smtps:example.com:465
/etc/postfix/master.cf: relay-smtps unix - - n - - smtp # Client-side SMTPS requires "encrypt" or stronger. -o smtp_tls_security_level=encrypt -o smtp_tls_wrappermode=yes
Use "postmap hash:/etc/postfix/transport" and "postfix reload" to make the change effective.
See SOHO_README for additional information about SASL authentication.
提供了兩種方法:
- 所有的外發郵件全部發送到一個特定的smtps服務器
- 根據不同的郵件地址轉發到不同的smtps服務器
我使用了我校校園郵箱(網易的企業郵箱)進行測試,(很遺憾QQ似乎沒有開465端口)。經過測試,發現方法一會被當成垃圾郵件,而且顯然不適用我們現在的情況(畢竟你不可能拿網易的服務器給別的郵箱做中轉),方法二正常發送。
PS. 建議先用telnet看下你的目的郵箱服務器開了哪個端口,別壓根沒開465...
獲取郵箱的交換服務器域名,即MX記錄。
假定我的校園郵箱是abcabcabc@xyz.edu.cn
,查詢MX記錄
nslookup -qt=MX xyz.edu.cn
xyz.edu.cn MX preference = 10, mail exchanger = hzmx02.mxmail.netease.com
xyz.edu.cn MX preference = 5, mail exchanger = hzmx01.mxmail.netease.com
xyz.edu.cn MX preference = 15, mail exchanger = mxhm.qiye.163.com
隨便記下來一個,比如hzmx01.mxmail.netease.com
。
按如下配置
/etc/postfix/main.cf:
transport_maps = hash:/etc/postfix/transport
/etc/postfix/transport:
xyz.edu.cn relay-smtps:hzmx01.mxmail.netease.com:465
/etc/postfix/master.cf:
relay-smtps unix - - n - - smtp
# Client-side SMTPS requires "encrypt" or stronger.
-o smtp_tls_security_level=encrypt
-o smtp_tls_wrappermode=yes
配置完后執行
postmap hash:/etc/postfix/transport
生成transport.db
重載配置
postfix reload
向xyz.edu.cn郵箱發送郵件,成功。
這樣配置后,想給哪個郵箱發郵件都得添加transport才行,很麻煩,但是別的辦法暫時沒找到可行的.......
按理說可以強制全走465端口的...
慢慢再找別的方法吧,現在先湊合着用吧...
3. 方法二 465端口最新配置方法(iptables)
按之前操作成功可以發郵件,但是總不能把所有的郵箱域名都寫到transport中,看了官方的transport文檔,transport的配置規則是domain transport:nexthop
,nexthop要么不寫,讓它自動去查DNS同時使用25端口;要么就指定域名和端口,無法在指定端口的同時使用DNS查詢MX記錄。
我想了想,ssl加密和465端口似乎沒必然的聯系,因為開了加密不指定端口的話還一直用25端口。那只能是在25端口上進行TLS/SMTPS通信。所以,我大膽操作,將25出端口直接映射到465端口,讓所有的都按默認走25端口(實際會被轉發到465),成功。具體操作如下:
/etc/poostfix/transport
修改:
youdomain.com :
.youdomain.com :
* relay-smtps:
更新postfix配置文件:
postmap hash:/etc/postfix/transport
postfix reload
設置端口映射,將出的25端口映射到465,我是centos7,以iptables為例。
安裝iptables.services,不然systemctl無法啟動iptables。
yum install iptables-services
配置端口映射
iptables -t nat -A OUTPUT -p tcp --dport 25 -j DNAT --to-destination :465
啟動iptables
systemctl start iptables
注意:啟動iptables后一定不要斷開ssh連接,先開另一個終端測試一下能否ssh連到服務器,因為iptables可能有預定義規則攔截,導致連不上新的ssh終端,不要問我為什么知道!如果發現無法登錄新的ssh終端,請立刻systemctl stop iptables,把里面的規則刪了再重來。
查看所有的iptables規則
iptables-save
刪除所有規則
iptables -F
這樣配置后,只要是在465端口開了SSL的郵箱就都能發,很多企業郵箱都開了,但是遭天譴的QQ郵箱只開了25端口...這就很遺憾,不能給QQ郵箱發郵件了。transport文件也可以靈活配置,對於有的不使用465端口的,可以手動添加到transport中走自己的端口。
方法二補充
時隔一年,又配了一次postfix,發現方法二完全不需要設置transport_maps和relay-smtps ,只要修改master.cf中的
smtp unix - - y - - smtp
這是默認的發郵件服務,在這行下面啟用tls即可,如下
smtp unix - - y - - smtp
-o smtp_tls_security_level=encrypt
-o smtp_tls_wrappermode=yes
-o smtp_tls_loglevel=1
然后同上設置iptables
番外篇
添加DNS記錄,讓郵件客戶端自動配置郵件服務器。主要是配置一些SRV記錄,在RFC6186中對這些記錄做出了規定,主要可以配置以下幾個:
_imap._tcp. IN SRV 10 10 143 mail.example.net.
_imaps._tcp. IN SRV 10 10 993 mail.example.net.
_pop3._tcp. IN SRV 10 10 110 mail.example.net.
_pop3s._tcp. IN SRV 10 10 995 mail.example.net.
_smtps._tcp. IN SRV 10 10 465 mail.example.net.
_submission._tcp. IN SRV 10 10 587 mail.example.net.
除了這些,也可以添加常見的子域名A記錄解析,如smtp./mail./pop3./imap./..... ,雖然郵件客戶端不一定去查,但是有些會去查,可以添加上。
更多關於自動配置的可參考
- Mail Client Auto-Configuration — Roll Your Own Network
- RFC6186
- How to configure email autoconfiguration for a domain? - Server Fault
關於windows 10 mail APP無法登錄dovecot pop3郵箱的解決辦法:
假如你的郵箱地址是apple@goodmail.com,在windows 10 郵件app添加賬戶時有個“用戶名”,這里只寫apple,而不能帶@goodmail.com。
可以使用openssl測試能否登錄:
openssl s_client -connect goodmail.com:995
....
+OK Dovecot (Ubuntu) ready.
user apple
+OK
pass yourpassword
+OK Logged in
密碼是明文密碼。