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

Mary-Kate Olsen
發布: 2024-10-27 00:03:30
原創
301 人瀏覽過

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中文網其他相關文章!

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