When setting up a mail server (like Postfix + Dovecot) or debugging email delivery, it’s often necessary to verify whether your SMTP host works correctly and if your credentials are valid.Here are two practical methods you can use with PHP 7.4. ✅ Method 1: Using PHPMailer (Recommended) PHPMailer is a full-featured mailer library supporting STARTTLS…
Category: PHP
About PHP
使用 PHP 測試 SMTP 功能(PHP 7.4 範例)
在開發郵件系統或部署郵件伺服器(如 Postfix、Dovecot)時,常常需要快速驗證 SMTP 主機是否可用、帳號密碼是否正確。本文提供兩種實用方式,使用 PHP 7.4 即可輕鬆測試郵件寄送與認證。 ✅ 方法一:使用 PHPMailer(推薦) 這是最直覺、最穩定的做法。PHPMailer 支援 STARTTLS / SMTPS / AUTH LOGIN,並能顯示詳細的 SMTP 除錯訊息。 安裝 若可使用 Composer: 範例程式:smtp_test.php 若無法使用 Composer,可直接下載 PHPMailer 的三個核心檔案:PHPMailer.php, SMTP.php, Exception.php,並在程式中手動引用。 ✅ 方法二:純 PHP Socket 測試(不發信,只測握手) 這個版本不寄信,只測試 連線 → STARTTLS → AUTH LOGIN 是否正常。非常適合快速檢查主機、TLS 或帳密問題。 🔍 常見錯誤與排查 問題 原因與建議 Connection timed out 主機或連接埠錯誤、防火牆阻擋 Peer certificate CN…