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

phplist + phpmailer 透過 Gmail SMTP 發送郵件完整配置教學:SSL 憑證、OpenSSL 啟用與除錯技巧

     編輯:PHP 相關知識

問題背景

phplist 內建 phpmailer 版本較舊(1.73,2005 年),不支援 Gmail 要求的 SSL/TLS 驗證,導致「Mailer Error: The following From address failed」錯誤,實際上是根本無法連上 SMTP 伺服器。

解決路徑總覽

  1. 升級 phpmailer 至 5.1+ 版本(支援 SSL)
  2. 啟用 PHP OpenSSL 擴充套件
  3. 調整 phplist 郵件類別設定 SSL 參數

步驟 1:驗證 phpmailer 獨立運作

下載 PHPMailer 5.1,建立測試腳本 test_gmail.php:

<?php
require 'class.phpmailer.php';
require 'class.smtp.php';

$mail = new PHPMailer();
$mail->SMTPDebug = 1;  // 關鍵:開啟除錯輸出
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = 'your@gmail.com';
$mail->Password = 'your_app_password';  // 需使用應用程式密碼
$mail->From = 'your@gmail.com';
$mail->FromName = 'Webmaster';
$mail->Subject = 'Test';
$mail->MsgHTML('Test body');
$mail->AddAddress('your@gmail.com');

if (!$mail->Send()) {
    echo "Error: " . $mail->ErrorInfo;
} else {
    echo "Sent!";
}

若出現 Unable to find the socket transport "ssl",表示 PHP 未啟用 OpenSSL。

步驟 2:啟用 PHP OpenSSL

Windows 環境

  1. 編輯 php.ini,取消註解:extension=php_openssl.dll
  2. 將 PHP 目錄下的 ssleay32.dll、libeay32.dll 複製到 C:\Windows\System32\(或加入系統 PATH)
  3. 重啟 Apache/IIS

Linux 環境

# Ubuntu/Debian
sudo apt-get install php-openssl
sudo systemctl restart apache2

# CentOS/RHEL
sudo yum install php-openssl
sudo systemctl restart httpd

步驟 3:更新 phplist 內建 phpmailer

  1. 用新版 class.phpmailer.php、class.smtp.php 覆蓋 lists/admin/phpmailer/ 同名檔案
  2. 編輯 lists/admin/class.phplistmailer.php 約第 36 行,在 $this->Host = PHPMAILERHOST; 前加入:
    $this->IsSMTP();        // 新增
    $this->SMTPAuth = true;
    $this->SMTPSecure = "ssl";  // 新增
    $this->Helo = getConfig("website");
    $this->Host = PHPMAILERHOST;
    $this->Port = 465;          // 新增:Gmail SSL 埠
  3. 確認 config.php 設定正確:
    define("PHPMAILERHOST", 'smtp.gmail.com');
    $phpmailer_smtpuser = 'your@gmail.com';
    $phpmailer_smtppassword = 'your_app_password';

關鍵除錯技巧

  • $mail->SMTPDebug = 1; 可輸出詳細 SMTP 交互過程,快速定位連線/認證/SSL 問題
  • Gmail 需開啟「兩步驟驗證」並產生「應用程式密碼」,不可使用登入密碼
  • 防火牆/雲端安全群組需開放輸出 465 埠

常見錯誤對照表

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
錯誤訊息原因解決
Unable to find socket transport "ssl"PHP 未載入 OpenSSL啟用 extension=php_openssl.dll,複製 DLL 到 System32
Connection timed out網路不通/防火牆阻擋檢查 465 埠連通性,雲端安全組規則
Authentication failed帳密錯誤/未用應用程式密碼產生 Gmail 應用程式密碼
From address failed舊版 phpmailer 不支援 SSL升級 phpmailer 5.1+,設定 SMTPSecure=ssl