Ubuntu Server 安裝 Postfix + pop3 imap

今天會學的技能是在Linux 上架設mail server,Postfix其實沒有想像中的複雜,基本的設定值也很簡單,比較難的是「寄信/收信遇到問題」的處理。

你一定覺得很奇怪,gmail 和 yahoo 信箱就很好用了,為何要自己去架設?因為facebook Workspace 只能使用公司的 Email。

Facebook Workplace 可以將同事連結在一起,方便大家交流意見,免費進行群組對話、傳送訊息、撥打語音/視訊電話,並享有無限儲存空間。專為企業設計的實用工具。

不自己架的話,可以直接花錢買gmail for bussiness,如果想省錢就花點時間自己架了。


安裝用指令:

sudo apt-get install postfix

啟動停止 Postfix 的指令如下:

sudo /etc/init.d/postfix start #啟動 Postfix
sudo /etc/init.d/postfix stop #停止 Postfix
sudo /etc/init.d/postfix restart #重新啟動 Postfix

Postfix 主要的組態設定檔是 /etc/postfix/main.cf。
編輯 main.cf 的指令如下:

sudo vi /etc/postfix/main.cf

Postfix一些常用的指令:

# 列出目前在 Mail Queue 中的信件
mailq

# 刪除所有在 Queue 中的郵件
postsuper -d ALL

# 刪除所有正在 deferred 佇列中的郵件 ( 刪除曾經發送失敗的信 )
postsuper -d ALL deferred

# 刪除所有正在 deferred 佇列中的郵件 ( 可看出哪些信被刪除了 )
find /var/spool/postfix/deferred -type f -exec rm -vf \{\} \;

# 刪掉「三天以前」無法發出的郵件
find /var/spool/postfix/deferred -type f -mtime +3 -exec rm -f \{\} \;

# 列出目前所有無法發出的郵件
find /var/spool/postfix/deferred -type f -exec ls -l –time-style=+%Y-%m-%d_%H:%M:%S {} \;

# 刪除超過 5 天的 “defer” 佇列中的退信紀錄
find /var/spool/postfix/defer -type f -mtime +5 -exec rm -f \{\} \;


檢查為什麼無法收到信:

vi /var/log/mail.log

進入 vi 後,按大寫G,移到 end of file,訊息:

postfix/smtp[6063]: DDA94BD4E4: to=[email protected], relay=gmail-smtp-in.l.google.com[74.125.24.26]:25, delay=0.88, delays=0.06/0.02/0.34/0.46, dsn=5.7.28, status=bounced (host gmail-smtp-in.l.google.com[74.125.24.26] said: 550-5.7.28 [167.172.78.3       1] Our system has detected an unusual rate of 550-5.7.28 unsolicited mail originating from your IP address. To protect our 550-5.7.28 users from spam, mail sent from your IP address has been blocked. 550-5.7.28 Please visit 550-5.7.28  https://support.google.com/mail/?p=UnsolicitedIPError to review our 550 5.7.28 Bulk Email Senders Guidelines. 31si2288714ple.177 - gsmtp (in reply to end of DATA command))

訊息被Google 擋掉。

解法,增加 TXT 到 DNS,DNS 提供網域名稱與IP位址對應的查詢,除了基本的 A、MX、CNAME、PTR、SRV 記錄外,還有一個 TXT 記錄,他可提供文字資訊給本身網域以外的來源一些額外的資訊,例如 SPF ( Sender Policy Framework )。

SPF 到底可以拿來幹嘛?

SPF 可以讓 收信端主機 根據來信中的 寄件者郵件網域,主動去寄信端所屬的DNS核對SPF記錄,當寄件者郵件網域 不同/不合法 於寄信主機網域時,可以丟棄、退信、或特別標註該封郵件。SPF 可以阻擋別人冒用你的網域寄信。

使用 dig 指令查詢網域的SPF設定,查詢資料類型請使用TXT。

dig @8.8.8.8 dropboxlike.com TXT

GMail 對被退信的說明:
https://support.google.com/mail/answer/81126?p=UnsolicitedIPError&visit_id=637161508037219534-3989246197&rd=1

為了盡可能減低郵件遭標示為垃圾郵件的機率,請設定以下驗證方式:

  • 為網域發布 SPF 記錄。SPF 可防止垃圾郵件發布者假借您網域的名義寄出未經授權的郵件。
  • 為郵件開啟 DKIM 簽署功能。收件伺服器會透過 DKIM 驗證網域擁有者是否確實傳送了郵件。重要資訊:Gmail 規定使用 1024 位元以上的 DKIM 金鑰。
  • 為網域發布 DMARC 記錄。DMARC 可協助寄件者防止自己網域的電子郵件遭到假冒。

為了讓 SPF 和 DKIM 順利驗證郵件,郵件的「From:」標頭必須與寄件網域相符。郵件則須通過 SPF 或 DKIM 檢查才算完成驗證。

請勿在未經授權的情況下冒用其他網域或寄件者名義傳送郵件。這種做法稱為「假冒」,可能會導致 Gmail 將郵件歸類為垃圾郵件。

設好 SPF 後新的 mail.log:

postfix/smtp[6598]: 54723BD202: to=<[email protected]>, relay=eforward3.registrar-servers.com[162.255.118.51]:25, delay=1, delays=0/0/0.82/0.2, dsn=5.1.1, status=bounced (host eforward3.registrar-servers.com[162.255.118.51] said: 554 5.1.1 <[email protected]>: Recipient address rejected: undeliverable address: host mail-outgoing-fef.relay.svc.cluster.local[10.27.5.108] said: 554 5.7.1 <[email protected]>: Relay access denied (in reply to RCPT TO command) (in reply to RCPT TO command))

解法:

因為DNS裡太多 MX 的欄位,刪一刪,只留下一組即可。


也可以使用 CheckTLS.com 的測試功能幫忙測試一下,填上主機上可收信的信箱,按下 Start Test 即可:
https://www.checktls.com/TestReceiver


如果 postfix 回報 Relay access denied;

那是因為 /etc/postfix/main.cf 的設定值:

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination

所以,如果不是從本機或內網或非授權的 domain name 那就會被拒絕,顯示 Relay access denied。

寄到Gmail 的信容易變「垃圾信」,所以一定要幫信建立規則(篩選器):


最後,如果你真的問題實在太多,真心建議改用Gmail API 來寄送:


安裝教學:

《分享》Ubuntu架設mail server
http://www.shunze.info/forum/thread.php?boardid=3&threadid=2015

使用 Ubuntu 安裝郵件伺服器 (Mail Server):Postfix + Dovecot + Openwebmail
http://jensoncc.blogspot.tw/2012/08/ubuntu-mail-serverpostfix-dovecot.html

Ubuntu Server 安裝 Postfix + pop3 imap
http://linadonis.pixnet.net/blog/post/7340997


外國人寫的也滿好的,看英文吃力很多,max心得是:很多地方看不懂就跳過就好了,重點是紅字的部份都要看懂即可:

Mark as Complete
How To Install and Configure Postfix on Ubuntu 16.04
https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-on-ubuntu-16-04

How To Set Up a Postfix E-Mail Server with Dovecot
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-postfix-e-mail-server-with-dovecot

How to Install and Configure Postfix as a Send-Only SMTP Server on Ubuntu 16.04
https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-as-a-send-only-smtp-server-on-ubuntu-16-04

Setup mail forwarding in postfix on Ubuntu or Debian
https://www.binarytides.com/postfix-mail-forwarding-debian/


連線加密,可以用 let encrypt:

Using let’s encrypt certs with postfix
http://stackoverflow.max-everyday.com/2017/02/handling-exceptions-tornado/

letsencrypt 的 certbot 的官方說明:
https://certbot.eff.org/#ubuntuxenial-other

certbot 指令用法:

certbot --help

新的版本的 certbot 用起來更簡單:

certbot certonly --cert-name your-domain-name

Max 是使用手動模式:

certbot certonly --manual -d mail.dropboxlike.com

畫面顯示:

可以另外再開一個 terminal 連進去伺服器裡,先使用 curl http://mail.dropboxlike.com/ 來看看有沒有顯示 certbot 所需要的內容。

Max 的指令:

 ➜  html cd .well-known 
 ➜  .well-known mkdir acme-challenge
 ➜  .well-known cd acme-challenge 
 ➜  acme-challenge echo "KECpMIVxKY_-bWY_gGY3Hqqwvj-q_Pi0wy0eJdvPNsw.wxfS4hcTbJJfl1qAmLveBUuXTdcQeGE04Mvfw4Ed1NA" > KECpMIVxKY_-bWY_gGY3Hqqwvj-q_Pi0wy0eJdvPNsw

透過 echo 把內容放進檔案裡後,再回到 certbot 按下 Enter

 Press Enter to Continue
 Waiting for verification…
 Cleaning up challenges
 IMPORTANT NOTES:
 Congratulations! Your certificate and chain have been saved at:
 /etc/letsencrypt/live/mail.dropboxlike.com/fullchain.pem
 Your key file has been saved at:
 /etc/letsencrypt/live/mail.dropboxlike.com/privkey.pem
 Your cert will expire on 2020-05-01. To obtain a new or tweaked
 version of this certificate in the future, simply run certbot
 again. To non-interactively renew all of your certificates, run
 "certbot renew"
 If you like Certbot, please consider supporting our work by:
 Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 Donating to EFF:                    https://eff.org/donate-le 

就成功拿到憑證了. ^_^


Max的參數設定 /etc/postfix/main.cf

# TLS parameters
smtpd_tls_cert_file=/etc/letsencrypt/live/mail.dropboxlike.com/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/mail.dropboxlike.com/privkey.pem
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

smtp_use_tls = yes
smtp_tls_note_starttls_offer = yes

上面紅字部份是我有修改的。

將 Let’s Encrypt 申請到的 SSL 憑證用在 Postfix 上,提供 SMTP with TLS 和 smtps 服務
https://ezbox.idv.tw/63/lets-encrypt-ssl-smtp-tls-smpts/


如果有其他 user 也要用,可以加入下面這行設定值:

local_recipient_maps = proxy:unix:passwd.byname $alias_maps

完整的設定值,可以參考 /etc/postfix/main.cf.proto

除了設定  postfix 還要去設  dovecot 打開 SSL 選項,

# nano /etc/dovecot/conf.d/10-ssl.conf

找到 ssl 這行,把 no 改成 yes

ssl = yes

並且把自己的私鑰和公鑰指向檔案的路徑

ssl_key =</etc/letsencrypt/live/mail.dropboxlike.com/privkey.pem
ssl_cert =</etc/letsencrypt/live/mail.dropboxlike.com/fullchain.pem

mail server debug 用的 log:

/var/log/mail.log


必學的 2個指令:

dig  your_domain

host your_domain

一定要確定 DNS 相關設定值是正確的。

如果你遇到任何的錯誤,請先確定連線的 ip 有對應到你指定的 server


遇到:How to correct Postfix’ ‘Relay Access Denied’? 處理

Diagnostic-Code: smtp; 554 5.7.1 <[email protected]>: Relay access denied

you now need to authorise your local network by adding it to mynetworks. For example,

mynetworks = 192.168.1.0/24 127.0.0.0/8

測試 POP3

telnet mail.dropboxmax.com 110
Trying 128.199.124.123...
Connected to mail.dropboxmax.com.
Escape character is '^]'.
+OK Dovecot ready.
USER max
-ERR [AUTH] Plaintext authentication disallowed on non-secure (SSL/TLS) connections.

centos dsn client 的設定值放在 /etc/resolv.conf 檔案裡,設定範例:

nameserver 8.8.8.8

上面的做法,在ubuntu 裡已過時,需要改用這個解法:

在 /etc/network/interfaces 檔裡,加入下面這一行就可以指定 DNS Server 了

dns-nameservers 8.8.8.8 168.95.192.1

滿奇怪的,這個設定檔好像需要內縮,沒有內縮,我使用 reboot 後,dns 設定值沒有被修改,內縮後reboot 即可取到正確的 DNS 設定。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *