WordPress 預設使用 `wp_mail()` (底層為 `PHPMailer`) 發送註冊郵件。若使用者收不到信,且已排除垃圾信箱、SMTP 服務正常,通常是伺服器環境配置問題:Linux 缺少 `sendmail`,Windows 預設 `mail()` 不支援 SMTP 驗證。
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 版 PHP `mail()` 不支援 SMTP 驗證,必須修改 WordPress 核心檔案改用 `PHPMailer` 的 SMTP 模式。
檔案:`
// 找到約第 550 行 (版本略有不同)
$phpmailer->IsMail();
// 改為
$phpmailer->IsSMTP();
檔案:`
尋找 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"; // 寄件人名稱
同檔案 `pluggable.php`,搜尋 `wordpress@` (約第 1057 行):
$wp_email = 'me@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
// 改為固定寄件人
$wp_email = 'noreply@yourdomain.com';
寄件人名稱同區塊修改:
$wp_name = 'Your Site Name';
在主題 `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 不會被覆蓋。
| 提供者 | SMTP Host | Port | 加密 | 備註 |
|---|---|---|---|---|
| Gmail | smtp.gmail.com | 587 | TLS | 需開啟「低安全性應用程式存取」或使用「應用程式密碼」 (2FA 帳號必須) |
| Outlook.com / Office 365 | smtp.office365.com | 587 | TLS | 帳號為完整信箱 |
| 企業 Exchange | smtp.corp.domain | 587/25 | TLS/StartTLS | 諮詢 IT 管理員 |
| SendGrid / Mailgun / Amazon SES | smtp.sendgrid.net 等 | 587/2525 | TLS | API Key 做為 Password |
WordPress 郵件發送問題 90% 源於伺服器環境與 PHP `mail()` 的限制。Linux 裝 `sendmail` 或 `postfix` 最省事;Windows 或追求高送達率、可追蹤、支援附件/HTML 郵件,**強烈建議改用 SMTP 模式並配合 `phpmailer_init` Hook 客製化**,避免修改核心檔案。若郵件量大,建議接入專業交易郵件服務 (SendGrid, Mailgun, Amazon SES, Postmark) 以獲得專業 IP 信譽、退信追蹤、開啟率統計等企業級功能。