為什麼我的 PHPMailer 給予「SMTP connect() Failed」錯誤?

Mary-Kate Olsen
發布: 2024-10-26 03:11:28
原創
867 人瀏覽過

Why Is My PHPMailer Giving Me the

SMTP connect() 失敗PHPMailer:解決PHP 中的問題

PHPMailer 是一個流行的PHP 庫,用於使用SMTP 來使用SM郵件。當您遇到「Mailer Error: SMTP connect() failed」錯誤時,表示與 SMTP 伺服器建立連線時發生問題。

了解錯誤

錯誤訊息「Mailer Error: SMTP connect() failed」表示 PHPMailer 無法連線到指定的 SMTP 伺服器。這可能是由於多種原因造成的,例如:

  • SMTP 設定不正確
  • 連接埠被封鎖
  • 伺服器相關問題
  • 防火牆設定

解決問題

要解決此問題,請依照下列步驟操作:

    確保程式碼中的SMTP 設定(主機、連接埠、使用者名稱和密碼)正確。 檢查連接埠阻止:
  1. 大多數 SMTP 伺服器使用連接埠 25、465 或 587安全連線。檢查您的伺服器上是否開啟這些連接埠。
  2. 檢查伺服器問題:
  3. 聯絡您的 SMTP 伺服器供應商,以確保不存在與伺服器相關的中斷或維護問題。
  4. 設定防火牆:
  5. 如果您的伺服器上設定了防火牆,請確保它允許必要的 SMTP 連接埠上的出站連線。
Gmail SMTP 的具體注意事項

如果您將Google 的SMTP 伺服器與PHPMailer 一起使用,請記住以下幾點:

Google 使用較新的OAuth 2.0 驗證機制。

    啟用「較不安全的應用程式」 " 在您的Google 帳戶設定中允許PHPMailer 進行連線。
  • 使用連接埠587 上的TLS,而不是連接埠465 上的SSL。
  • 使用Google SMTP 的範例程式碼

以下是程式碼的修訂版本,其中包括Gmail SMTP 的必要變更:

透過實施這些措施,您應該能夠解決「SMTP connect() failed」問題" 錯誤並使用PHPMailer 成功發送電子郵件。
<code class="php">require "class.phpmailer.php";
$mail = new PHPMailer(); 
$mail->IsSMTP();                              // send via SMTP
$mail->Host = "tls://smtp.gmail.com";
$mail->SMTPAuth = true;                       // turn on SMTP authentication
$mail->Username = "[email protected]";        // SMTP username
$mail->Password = "mypassword";               // SMTP password
$webmaster_email = "[email protected]";       //Reply to this email ID
$email="[email protected]";                // Recipients email ID
$name="My Name";                              // Recipient's name
$mail->From = $webmaster_email;
$mail->Port = 587;
$mail->FromName = "My Name";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"My Name");
$mail->WordWrap = 50;                         // set word wrap
$mail->IsHTML(true);                          // send as HTML
$mail->Subject = "subject";
$mail->Body = "Hi,
This is the HTML BODY ";                      //HTML Body 
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body 

if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?></code>
登入後複製

以上是為什麼我的 PHPMailer 給予「SMTP connect() Failed」錯誤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!