雖然只是在整GITLAB過程中的一個小插曲,卻意外的難搞,
花了我很多時間,網絡上的各種教程與排錯衆說紛紜,看似簡單的功能變得異常複雜。
郵件發送功能, 即MTA做爲許多系統的必備,最常用的有sendmail與postfix兩種。
我使用的是Centos7 minimal版,postfix是與系統一起安裝進來的。
鑑於想更全面一些瞭解郵件發送軟件,就從鼎鼎大名的sendmail開始入手搗鼓。
Sendmail
<=========================================
一. 安裝sendmail
yum -y install sendmail sendmail-cf
二. 安裝MTA功能測試用軟件
yum -y install mailx php
三. 切換系統的郵件發送接口
alternatives --config mta
畫面顯示:
There are 2 programs which provide 'mta'.
Selection Command
-----------------------------------------------
+ 1 /usr/sbin/sendmail.postfix
* 2 /usr/sbin/sendmail.sendmail
Enter to keep the current selection[+], or type selection number: 2
輸入2後回車即把MTA功能切換到sendmail上,+號會顯示在sendmail的行頭。
四. 配置sendmail
vi /etc/mail/sendmail.mc
DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
把Addr的值從127.0.0.1修改爲0.0.0.0,不限制使用MTA的IP。
m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
生成正式的配置文件。
五. 重啓並測試功能
reboot -f
重啓完成後確認MTA程序已經切換到sendmail
ps aux | grep sendmail
root 1003 0.0 0.2 88688 2280 ? Ss 10:40 0:00 sendmail: accepting connections
smmsp 1018 0.0 0.1 84120 1912 ? Ss 10:40 0:00 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue
root 1141 0.0 0.0 112660 968 pts/1 R+ 10:51 0:00 grep --color=auto sendmail
sendmail的相關進程已經啓動
ps aux | grep postfix
root 1161 0.0 0.0 112660 968 pts/1 R+ 11:04 0:00 grep --color=auto postfix
postfix的相關進程都沒有開啓
用PHP函數發送郵件
php -a
在PHP的交互界面下輸入以下函數
mail('[email protected]', "Test email content", "sendmail title", null, "-f [email protected]");
* 使用PHP接口做測試的好處是可以隨意指定發送方的郵件地址,即mail函數的最後一個參數。
即使系統的hostname未設置也可以正常發送出郵件。
六. 使用linux的mail命令發送郵件
mail命令就沒有使用自定義的郵件發送地址,而是使用HOSTNAME。
安裝系統時由於沒有對hostname做特別設置,HOSTNAME的值是默認的 localhost.localdomain
這樣的郵件域名會被大多數郵箱如163,QQ拒收。
查看郵件發送log會發現以下錯誤
cat /var/log/maillog
dsn=4.1.8, stat=Deferred: 450 4.1.8 <[email protected]>: Sender address rejected: Domain not found
修改HOSTNAME
vi /etc/hosts
在最後加上一行
192.168.2.108 intest.com
這裏的IP地址是我跑sendmail虛擬機的IP,需根據實際情況設置
* 其實這個文件hosts只是用來設置本地路由表,但填上本機IP時,系統在啓動初始化中查到本機IP在hosts中,
就會用hosts文件中對應的域名來設置HOSTNAME。
重新啓動
reboot -f
重啓後發現本地的DNS配置文件etc/resolv.conf 已經被自動更新。
內容變成 nameserver 192.168.2.1
執行mail命令發送郵件
echo "test mail content"|mail -s "Mail title" [email protected]
==========================================>
Postfix
<=========================================
一. 切換系統的郵件發送接口
alternatives --config mta
* Postfix是Centos7系統默認自帶。 也可以用命令 yum list installed | grep postfix 確認 選擇postfix所在行的編號後回車
二. 重啓並測試功能
reboot -f
重啓後查看進程看到postfix相關的進程已經啓動
ps aux | grep postfix
root 1093 0.0 0.2 89544 2172 ? Ss 08:55 0:00 /usr/libexec/postfix/master -w
postfix 1094 0.0 0.4 89648 4016 ? S 08:55 0:00 pickup -l -t unix -u
postfix 1095 0.0 0.4 89716 4044 ? S 08:55 0:00 qmgr -l -t unix -u
postfix 1237 0.0 0.4 89796 4072 ? S 09:08 0:00 cleanup -z -t unix -u
postfix 1238 0.0 0.4 89652 4024 ? S 09:08 0:00 trivial-rewrite -n rewrite -t unix -u
postfix 1239 0.0 0.4 89856 4272 ? S 09:08 0:00 smtp -t unix -u
root 1274 0.0 0.0 112660 972 pts/1 R+ 09:09 0:00 grep --color=auto postfix
系統的hostname已經在sendmail配置的第六步中完成了配置,這裏就直接使用PHP與mail命令
兩種方法做測試。
php -a
mail('[email protected]', "Test email No1", "postfix mail", null, "-f [email protected]");
* php的mail函數可以隨意指定發送地址
echo "test mail"|mail -s "postfix mail title" [email protected]
==========================================>