2017-10-06 229 views
-2

在這個腳本完美運行之前,我最近三天面臨這個問題。現在收到錯誤:SMTP連接失敗

SMTP ERROR: Failed to connect to server: (0) 2017-10-06 21:05:34 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message was not sent.Mailer error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting [email protected]

這裏是我的腳本:

require 'PHPMailer/PHPMailerAutoload.php'; 

$mail = new PHPMailer; 
$mail->SMTPDebug = 2;     // Enable verbose debug output 
$mail->isSMTP();      // Set mailer to use SMTP 
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers 
$mail->SMTPAuth = true;    // Enable SMTP authentication 
$mail->Username = '[email protected]'; // SMTP username 
$mail->Password = 'mypassword';  // SMTP password 
$mail->SMTPSecure = 'tls';    // Enable TLS encryption, `ssl` also accepted 
$mail->Port = 587; 
$mail->setFrom('[email protected]', 'Your Name'); 
$mail->addAddress('[email protected]', 'My Friend'); 
$mail->Subject = 'First PHPMailer Message'; 
$mail->Body  = 'Hi! This is my first e-mail sent through PHPMailer.'; 
if (!$mail->send()) { 
    echo 'Message was not sent.'; 
    echo 'Mailer error: ' . $mail->ErrorInfo; 
} else { 
    echo 'Message has been sent.'; 
} 

我認爲Gmail可能已發送使用SMTP,或類似的東西電子郵件更改的設置。

+3

這裏有一個大膽的想法 - 嘗試在錯誤信息,告訴你所有關於這個問題以及如何解決它的鏈接如下。 – Synchro

回答

0

最後我能夠從本地主機發送電子郵件。這是我的代碼。

要安裝:

  1. 下載PHPMailer的
  2. 將它添加到您的項目(我把它放在根目錄)
  3. 添加自動加載類的腳本。
  4. 休息的代碼如下

     require "PHPMailer/PHPMailerAutoload.php"; 
         $mail = new PHPMailer; 
         $mail->SMTPOptions = array(
          'ssl' => array(
           'verify_peer' => false, 
           'verify_peer_name' => false, 
           'allow_self_signed' => true 
          ) 
         ); 
         //$mail->SMTPDebug = 2;         // Enable verbose debug output 
         $mail->isSMTP();          // Set mailer to use SMTP 
         $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers 
         $mail->SMTPAuth = true;        // Enable SMTP authentication 
         $mail->Username = '[email protected]';     // SMTP username 
         $mail->Password = 'securepass';       // SMTP password 
         $mail->SMTPSecure = 'ssl';       // Enable TLS encryption, `ssl` also accepted 
         $mail->Port = 465;         // TCP port to connect to 
         //Recipients 
         $mail->setFrom('[email protected]', "Mailer"); 
         $mail->addAddress("[email protected]","receiver Name"); 
         $mail->isHTML(true);         
         $mail->Subject = "Subject"; 
         $mail->Body = "Body"; 
         $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 
          if($mail->send()){ 
          return array("msg"=>msg("success","Email has been sent.<br>")); 
          } else { 
           return array("msg"=>msg("error","Email can't send.Try Again<br>")); 
          } 
    
相關問題