首頁  >  文章  >  後端開發  >  如何修復 PHPmailer 中的「Mailer 錯誤:SMTP connect() 失敗」?

如何修復 PHPmailer 中的「Mailer 錯誤:SMTP connect() 失敗」?

Mary-Kate Olsen
Mary-Kate Olsen原創
2024-10-27 00:03:30302瀏覽

How to Fix

PHPmailer 中的SMTP Connect() 故障排除

簡介

簡介

解決方案

要解決此問題,請按照以下步驟操作:

    1.在Google 帳戶中啟用不太安全的應用程式
  • 登入您的Google 帳戶並導航至:https://www.google.com/settings/security/lesssecureapps

確保啟用「開啟對不太安全的應用程式的存取”選項。

    2.使用連接埠587 上的TLS 而不是連接埠465 上的SSL
  • <code class="php">$mail->Host = "ssl://smtp.gmail.com";
    $mail->Port = 465;</code>

    修改程式碼以使用連接埠587 上的連接埠587 上的連接埠587 上的連接埠587 TLS 而不是連接埠465 上的SSL。取代以下行:

    <code class="php">$mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;</code>
  • 與:

範例程式碼

<code class="php">require "class.phpmailer.php";
$mail = new PHPMailer(); 
$mail->IsSMTP();                              // send via SMTP
$mail->SMTPDebug = 2;  // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true;                       // turn on SMTP authentication
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->SMTPAutoTLS = false;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = "[email&#160;protected]";        // SMTP username
$mail->Password = "mypassword";               // SMTP password
$webmaster_email = "[email&#160;protected]";       //Reply to this email ID
$email="[email&#160;protected]";                // Recipients email ID
$name="My Name";                              // Recipient's name
$mail->From = $webmaster_email;
$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 成功發送電子郵件。

以上是如何修復 PHPmailer 中的「Mailer 錯誤:SMTP connect() 失敗」?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn