SMTP Error: Could not connect to SMTP host. send fail
現象:IsSMTP() 內部設置 $this->Mailer = 'smtp' (小寫),但 switch($this->Mailer) 中 case 'smtp': 區分大小寫,導致落入 default: 分支,改用 MailSend() (本地 mail() 函數) 而非真正 SMTP。
// class.phpmailer.php
function IsSMTP() {
$this->Mailer = 'SMTP'; // 改為大寫
}
此問題存在於舊版 PHPMailer (5.x 早期),新版 (6.x+) 已修復。若仍用舊版建議升級。
原因:安全策略禁用 fsockopen,導致 SMTP 連接建立失敗。
// class.smtp.php
$this->smtp_conn = @pfsockopen($host, $port, $errno, $errstr, $tval);
$this->smtp_conn = @stream_socket_client(
"tcp://{$host}:{$port}",
$errno, $errstr, $tval,
STREAM_CLIENT_CONNECT
);
telnet smtp.exmail.qq.com 465 或 openssl s_client -connect smtp.gmail.com:465。iptables -L -n / Windows 防火牆出站規則。systemctl stop firewalld),確認為防火牆問題後再精準放行。| 步驟 | 命令/操作 | 預期結果 |
|---|---|---|
| 1. PHPMailer 版本 | composer show phpmailer/phpmailer | ≥ 6.5.0 (含大小寫修復) |
| 2. 函數可用性 | php -r "var_dump(function_exists('fsockopen'));" | bool(true) |
| 3. 端口連通性 | telnet smtp.host 465 | Connected / 顯示 220 banner |
| 4. SSL/TLS 驗證 | openssl s_client -connect smtp.host:465 | Certificate chain 正常 |
| 5. PHPMailer 參數 | Host/Port/SMTPSecure/Username/Password | 與郵件服務商文檔一致 |