首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

IIS 環境下 PHP mail() 函數透過 SendMail 發送郵件的完整設定指南(官方推薦做法)

     編輯:PHP 綜合知識

前言

在 Windows IIS 環境下,PHP 內建的 mail() 函數預設使用本機 SMTP 服務,但許多伺服器並未架設郵件伺服器。官方推薦的解決方案是使用 SendMail for Windows 作為郵件傳遞代理,透過外部 SMTP(如 QQ 郵箱、163 郵箱)發送郵件。本文詳細記錄從下載、安裝到 php.ini 與 sendmail.ini 設定的完整步驟。

一、下載並安裝 SendMail

  1. 前往官方網站 https://glob.com.au/sendmail/ 下載 sendmail.zip。
  2. 解壓縮至任意目錄,例如 D:\php\sendmail\。

二、修改 php.ini 啟用 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 使設定生效。

三、設定 sendmail.ini 連接外部 SMTP

進入 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

儲存檔案。

四、測試與常見問題

  1. 重新啟動 IIS(或執行 iisreset)。
  2. 撰寫簡單 PHP 測試腳本呼叫 mail() 發送測試信。
  3. 若無法發送,請檢查:
    • 防火牆/安全軟體(如 McAfee)是否封鎖 25、465、587 埠。
    • SMTP 伺服器位址、帳號密碼是否正確。
    • PHP 錯誤日誌是否有 sendmail 相關錯誤訊息。

結語

完成上述步驟後,PHP mail() 即可透過 SendMail 穩定發送郵件。此做法為官方推薦,適用於無法架設本機 SMTP 服務的 Windows IIS 環境。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved