在 Windows IIS 環境下,PHP 內建的 mail() 函數預設使用本機 SMTP 服務,但許多伺服器並未架設郵件伺服器。官方推薦的解決方案是使用 SendMail for Windows 作為郵件傳遞代理,透過外部 SMTP(如 QQ 郵箱、163 郵箱)發送郵件。本文詳細記錄從下載、安裝到 php.ini 與 sendmail.ini 設定的完整步驟。
sendmail.zip。D:\php\sendmail\。打開 php.ini,找到 [mail function] 區段,預設內容如下:
[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25
; For Win32 only.
;sendmail_from = me@example.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; sendmail_path = ""
; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =
請依以下方式修改:
SMTP = localhost 与 smtp_port = 25 前加分號 ; 註解掉。sendmail_path 註解,並指向剛才安裝的 sendmail.exe:[mail function]
; For Win32 only.
;SMTP = localhost
;smtp_port = 25
; For Win32 only.
;sendmail_from = me@example.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = "d:\php\sendmail\sendmail.exe -t"
; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =
注意: 路徑請依實際安裝位置調整,雙引號不可省略。儲存後重新啟動 IIS 使設定生效。
進入 D:\php\sendmail\ 目錄,編輯 sendmail.ini,主要填寫下列四項:
smtp_server=smtp.ym.163.com ; SMTP 伺服器位址(以 163 企業郵箱為例)
auth_username=info@yourdomain.com ; 郵箱完整帳號
auth_password=yourpassword ; 郵箱密碼(或授權碼)
force_sender=info@yourdomain.com ; 強制寄件人位址,建議與帳號一致
此外,啟用 SSL/TLS 安全連線:找到 ; auto = use SSL for port 465, otherwise try to use TLS,移除前方分號:
auto = use SSL for port 465, otherwise try to use TLS
儲存檔案。
iisreset)。mail() 發送測試信。sendmail 相關錯誤訊息。完成上述步驟後,PHP mail() 即可透過 SendMail 穩定發送郵件。此做法為官方推薦,適用於無法架設本機 SMTP 服務的 Windows IIS 環境。