萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> 程式語言綜合

WordPress 註冊郵件收不到?Linux/Windows 環境完整排查與 SMTP 修正指南

問題現象

WordPress 預設使用 `wp_mail()` (底層為 `PHPMailer`) 發送註冊郵件。若使用者收不到信,且已排除垃圾信箱、SMTP 服務正常,通常是伺服器環境配置問題:Linux 缺少 `sendmail`,Windows 預設 `mail()` 不支援 SMTP 驗證。

方案一:Linux (CentOS/Ubuntu) 啟用 sendmail

檢查服務狀態

systemctl status sendmail    # CentOS 7+
service sendmail status      # CentOS 6 / Ubuntu

啟用並設定開機自動啟動

# CentOS 7+
systemctl start sendmail
systemctl enable sendmail

# CentOS 6
/etc/init.d/sendmail start
chkconfig sendmail on

# Ubuntu/Debian
apt-get update && apt-get install -y sendmail
sendmailconfig  # 互動式配置
systemctl restart sendmail

測試發信

echo "Test body" | mail -s "Test Subject" user@example.com

若收到信,WordPress 即可正常發送。

方案二:Windows 環境改用 SMTP (推薦)

Windows 版 PHP `mail()` 不支援 SMTP 驗證,必須修改 WordPress 核心檔案改用 `PHPMailer` 的 SMTP 模式。

修改 1:切換至 SMTP 模式

檔案:`/wp-includes/pluggable.php`

// 找到約第 550 行 (版本略有不同)
$phpmailer->IsMail();

// 改為
$phpmailer->IsSMTP();

修改 2:設定 SMTP 參數

檔案:`/wp-includes/class-phpmailer.php`

尋找 var $Host 等變數定義區 (約第 50-80 行),修改為:

var $Host       = "smtp.example.com";      // SMTP 伺服器,如 smtp.gmail.com
var $Port       = 587;                     // 465 (SSL) 或 587 (TLS)
var $SMTPSecure = "tls";                   // "ssl" 或 "tls"
var $SMTPAuth   = true;                    // 需要驗證
var $Username   = "your_email@example.com"; // 完整信箱
var $Password   = "your_app_password";      // 應用程式密碼 (非登入密碼)
var $From       = "your_email@example.com"; // 寄件人信箱
var $FromName   = "Your Site Name";        // 寄件人名稱

修改 3:修正寄件人資訊 (避免「由 *** 代發」)

同檔案 `pluggable.php`,搜尋 `wordpress@` (約第 1057 行):

$wp_email = 'me@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));

// 改為固定寄件人
$wp_email = 'noreply@yourdomain.com';

寄件人名稱同區塊修改:

$wp_name = 'Your Site Name';

替代方案:不修改核心檔,使用 `phpmailer_init` Hook (推薦)

在主題 `functions.php` 或客製外掛中加入:

add_action('phpmailer_init', 'my_smtp_config');
function my_smtp_config($phpmailer) {
    $phpmailer->isSMTP();
    $phpmailer->Host       = 'smtp.example.com';
    $phpmailer->Port       = 587;
    $phpmailer->SMTPSecure = 'tls';
    $phpmailer->SMTPAuth   = true;
    $phpmailer->Username   = 'your_email@example.com';
    $phpmailer->Password   = 'your_app_password';
    $phpmailer->From       = 'noreply@yourdomain.com';
    $phpmailer->FromName   = 'Your Site Name';
}

優點:不修改核心,升級 WordPress 不會被覆蓋。

進階:Gmail / Outlook / 企業郵箱 專用設定

提供者SMTP HostPort加密備註
Gmailsmtp.gmail.com587TLS需開啟「低安全性應用程式存取」或使用「應用程式密碼」 (2FA 帳號必須)
Outlook.com / Office 365smtp.office365.com587TLS帳號為完整信箱
企業 Exchangesmtp.corp.domain587/25TLS/StartTLS諮詢 IT 管理員
SendGrid / Mailgun / Amazon SESsmtp.sendgrid.net 等587/2525TLSAPI Key 做為 Password

常見問題排除

修改後仍收不到信
檢查 `/wp-content/debug.log` (需在 `wp-config.php` 開啟 `WP_DEBUG_LOG`),搜尋 `PHPMailer` 錯誤訊息。
中文寄件人名稱亂碼
確保 `functions.php` / `pluggable.php` 以 UTF-8 無 BOM 編碼儲存 (Notepad++ / VS Code 可轉換)。
連線逾時
防火牆封鎖 587/465 埠,或雲端主機商限制 SMTP 埠 (AWS EC2 預設封鎖 25 埠,需申請解封或改用 587)。
SSL 憑證驗證失敗
加入 `$phpmailer->SMTPOptions = ['ssl' => ['verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true]];` (僅測試環境)。

結語

WordPress 郵件發送問題 90% 源於伺服器環境與 PHP `mail()` 的限制。Linux 裝 `sendmail` 或 `postfix` 最省事;Windows 或追求高送達率、可追蹤、支援附件/HTML 郵件,**強烈建議改用 SMTP 模式並配合 `phpmailer_init` Hook 客製化**,避免修改核心檔案。若郵件量大,建議接入專業交易郵件服務 (SendGrid, Mailgun, Amazon SES, Postmark) 以獲得專業 IP 信譽、退信追蹤、開啟率統計等企業級功能。

copyright © 萬盛學電腦網 all rights reserved