萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

LNMP 環境啟用 PHP mail() 發信功能並解決發信緩慢問題

PHP mail() 需系統安裝 sendmail 組件。本文記錄 LNMP 環境安裝 sendmail、測試發信、解決 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!';
?>

解決 sendmail 啟動極慢 (常見主機名解析問題)

編輯 /etc/hosts,將預設行:

127.0.0.1 localhost.localdomain localhost

修改為 (在末尾加入主機名稱):

127.0.0.1 localhost.localdomain localhost your-hostname

查看主機名稱指令:hostname

修改後重啟 sendmail:

service sendmail restart

發信速度即可從數分鐘縮短至秒級,網站評論郵件通知恢復即時送達。

copyright © 萬盛學電腦網 all rights reserved