用 CentOS 7.0 + PHP + Apache 建了個伺服器,並且建立了網站有自己的網址,不過寄出去email時,都會被 Gmail 判定為不安全,以致於所有信件都有一個小紅鎖頭在那麼警示(如下圖)。網路找了好久找到的辦法都不能用,後來總算找到解答,就把心得寫了下來。
當中的機制很複雜,我也不見得完全瞭解,但就我理解的範圍,是因為沒有把網址SSL的憑證加進postfix讓他也讀取,所以postfix的email寄出時都沒加密,就被Gmail判定不安全。所以,接下來要做的就是把憑證加進去
編輯 /etc/postfix/main.cf 加幾行字
smtpd_tls_security_level = may
smtp_tls_security_level = may
#以下的 pki/tls/ 路徑大家擺放憑證的位置可能不一樣,就找出你憑證的路徑換掉。如果你連網址憑證放哪裏都不知道,可以用搜尋的方式找找,另外CApath或cert_file副檔名有可能是不是.pem而是 .crt ,視各位情況不同
smtp_tls_CApath = /etc/pki/tls/certs
smtp_tls_cert_file = /etc/pki/tls/certs/postfix.pem
smtp_tls_key_file = /etc/pki/tls/private/postfix.key
以上這步驟,憑證就添加完成
接下來是開放一些權限,讓我的郵件伺服器prostfix可以讀取
用命令列執行以下
chown root:postfix /etc/pki/tls/certs
chmod 644 /etc/pki/tls/certs
chown root:postfix /etc/pki/tls/certs/postfix.pem
chmod 640 /etc/pki/tls/certs/postfix.pem
chown root:postfix /etc/pki/tls/private/postfix.key
chmod 644 /etc/pki/tls/private/postfix.key
在PHP的寄件程式,我用的是sendmail這個功能
假設原本是的寄件參數是這樣
$params = array('host' => 'localhost',
'port' => '25',
'auth' => false,
'username' => 'yourusername', // SMTP 帳號
'password' => 'yourpassword', // SMTP 密碼
'persist' => true,
'debug'=>true);
要多加一行字 'secure' => 'tls' ,改完結果如下...
$params = array('host' => 'localhost',
'port' => '25',
'auth' => false,
'username' => 'yourusername', // SMTP 帳號
'password' => 'yourpassword', // SMTP 密碼
'persist' => true,
'secure' => 'tls', // 這會啟用 TLS 加密
'debug'=>true);
最後,重啟伺服器,在命令列輸入
systemctl reload postfix
systemctl restart postfix
這樣就全部搞定了,重寄一封信給任一Gmail信相,再看到的郵件,小紅鎖就不見了。雖然感覺步驟不多,可是花了我半年的時間才搞定。網路論譠大家各說各的,有些資料也很舊,試了也錯。經過半年不斷嘗試,並詢問AI好幾十次來回對話,才找到這個最佳解法。
