簡體   English   中英

消息SMTP connect()的未捕獲異常'PHP mailer Exception'失敗

[英]Uncaught exception 'PHP mailer Exception' with message SMTP connect() failed

我收到此錯誤

致命錯誤:消息“ SMTP connect()”未捕獲的異常“ phpmailerException”失敗。 https://github.com/PHPMailer/PHPMailer/wiki/在C:\\ xampp \\ htdocs \\ muhasibb \\ epaper \\ PHPMailer-master \\ class.phpmailer.php:1465中對問題進行故障排除:#0 C:\\ xampp \\ htdocs \\ muhasibb \\ epaper \\ PHPMailer-master \\ class.phpmailer.php(1301):PHPMailer-> smtpSend('Date:Tue,17 O ...','This is Yousaf ...')#1 C:\\ xampp \\ htdocs \\ muhasibb \\ epaper \\ PHPMailer-master \\ class.phpmailer.php(1181):PHPMailer-> postSend()#2 C:\\ xampp \\ htdocs \\ muhasibb \\ epaper \\ 1.php(33):PHPMailer-> send ()在第1465行的C:\\ xampp \\ htdocs \\ muhasibb \\ epaper \\ PHPMailer-master \\ class.phpmailer.php中拋出#3 {main}

當我使用代碼時

<?php 
                            // Passing `true` enables exceptions

    //Server settings

    require 'PHPMailer-master/PHPMailerAutoload.php';
    $mail = new PHPMailer(true);                                // Enable verbose debug output
    $mail->isSMTP(); 
    $mail->SMTPDebug = 2;                                     // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'yousaf.farooq906@gmail.com';                 // SMTP username
    $mail->Password = '********';                           // SMTP password
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;
    $mail->Mailer = "smtp";                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('yousaf.farooq906@gmail.com', 'Yousaf Farooq');
    $mail->addAddress('yousaf.farooq906@gmail.com', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional


    //Attachments
        // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Yousaf Farooq';
    $mail->Body    = 'This is Yousaf Farooq';


    if($mail->send())
    echo 'Message has been sent';
    else
    echo 'Message could not be sent.';
?>

嘗試設置您的

$mail->SMTPSecure

$mail->SMTPSecure = 'tls';

另外,為您的Google帳戶創建應用密碼,並用它來代替$mail->Password

這段代碼對我有用

$mail = new PHPMailer(true); // create a new object

我評論了這一行// $ mail-> isSMTP(); //啟用SMTP

 $mail->SMTPDebug = 4; 
 $mail->SMTPAuth = true; // authentication enabled

您必須使用tls和587端口

 $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
 $mail->Host = "smtp.gmail.com";
 $mail->Port = 587; 
 $mail->Mailer = "smtp";
 $mail->isHTML(true);
 $mail->Username = "yourmail@gmail.com";
 $mail->Password = "yourpassword";
 $mail->From="yourmail@gmail.com";
 $mail->FromName="YOUR NAME";
 $mail->Subject = $subject;
 $mail->Body = $body;
 $mail->addAddress("somemail@gmail.com");

 if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
   echo "Email Sent";
 }

另外,當您使用gmail時,請轉到具有帳戶訪問權限的應用並設置“允許不太安全的應用”:

郵件配置

如果在本地主機上工作,

在php.ini中:刪除“ sendmail_from = postmaster @ localhost”前面的分號,然后重新啟動xampp。

在gmail帳戶中:在安全設置中,打開“允許安全程度較低的應用”。

最后在PHP腳本中:添加兩個函數:“ date_default_timezone_set('Etc / UTC')”“ gethostbyname('ssl://smtp.gmail.com')”作為“ $ mail-> Host”的值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM