PHP mail() 需系統安裝 sendmail 組件。本文記錄 LNMP 環境安裝 sendmail、測試發信、解決 sendmail 啟動緩慢的完整步驟。
yum install sendmail
chkconfig sendmail on
/etc/init.d/sendmail start
/root/lnmp restart # 重啟 LNMP 使 PHP 載入新環境
<?php
$txt = "hello";
$mail = 'your@email.com';
mail($mail, "My subject", $txt);
echo 'message was sent!';
?>
編輯 /etc/hosts,將預設行:
127.0.0.1 localhost.localdomain localhost
修改為 (在末尾加入主機名稱):
127.0.0.1 localhost.localdomain localhost your-hostname
查看主機名稱指令:hostname
修改後重啟 sendmail:
service sendmail restart
發信速度即可從數分鐘縮短至秒級,網站評論郵件通知恢復即時送達。