2017-12-11 5 views
-1

Phpmailer Konfiguration: Lumen.Lumen phpmailer: SMTP FEHLER: Verbindung zum Server konnte nicht hergestellt werden: (0)

Bisher arbeitet es wurde? Aber jetzt ist die gleiche Konfiguration throw Verbindung zum Server fehlgeschlagen:

Ich bin ein Neuling auf Laravel/Lumen Rahmen. Das ist meine PHPmailer-Konfiguration, ich weiß nicht, was ich hier falsch mache. Bitte helfen Sie mir hier.

<?php 

namespace App\Repositories; 
use App\Repositories\BaseRepository; 
use App\Models\ForgetModel; 
use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\SMTP; 
class ForgetRepository extends BaseRepository{ 
private $forget; 

public function __construct(ForgetModel $forget) { 
    $this->forget = $forget; 
} 

public function save_verification_code($email,$verification_code) 
{ 
    $query = $this->forget->onMaster() 
          ->insert(array('email'=>$email,'code'=>$verification_code)); 
} 

public function send_forget_password_email($to,$message) 
{ 

    $subject = 'Verification code to reset your password'; 
    $from = '[email protected]'; 
    $body = $message; 
    $headers = 'From: ' . strip_tags($from) . '\r\n'; 
    $headers .= 'MIME-Version: 1.0\r\n'; 
    $headers .= 'Content-Type: text/html; charset=ISO-8859-1\r\n'; 

    $mail = new PHPMailer(); // create a new object 
    $mail->IsSMTP(); // enable SMTP 
    $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only 
    $mail->SMTPAuth = true; // authentication enabled 
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail 
    $mail->Host = 'smtp.gmail.com'; 
    $mail->Port = 465; // or 587 
    $mail->IsHTML(true); 
    $mail->Username = '[email protected]'; 
    $mail->Password = '123456'; 
    $mail->SetFrom($from); 
    $mail->Subject = $subject; 
    $mail->Body = $message; 
    $mail->AddAddress($to); 

    if(!$mail->Send()) { 
     echo 'Mailer Error: ' . $mail->ErrorInfo; 
    } else { 
     //echo "Message has been sent"; 
    } 
    return true; 
} 
} 
+0

die Dokumentation lesen, suchen vor der Veröffentlichung. – Synchro

+0

Ich weiß nicht, was ich falsch mache. Ich habe Suche vor dem Posten der Frage, weiß nicht, wo das Problem ist, wie ich sagte, ich bin neu in PHP und Frameworks ... Bitte helfen Sie mir, wenn Sie @Synchro können –

Antwort

0
1. run the command "composer require phpmailer/phpmailer" in your cmd. 



namespace App\Repositories; 
use App\Repositories\BaseRepository; 
use App\Models\ForgetModel; 
use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\Exception; 

require '../vendor/phpmailer/phpmailer/src/Exception.php'; 
require '../vendor/phpmailer/phpmailer/src/PHPMailer.php'; 
require '../vendor/phpmailer/phpmailer/src/SMTP.php'; 

class ForgetRepository extends BaseRepository{ 
private $forget; 

public function __construct(ForgetModel $forget) { 
    $this->forget = $forget; 
} 

public function save_verification_code($email,$verification_code) 
{ 
    $query = $this->forget->onMaster() 
          ->insert(array('email'=>$email,'code'=>$verification_code)); 
} 

public function send_forget_password_email($to,$message) 
{ 
$mail = new PHPMailer(true); 
    try { 
     //Server settings 
     $mail = new PHPMailer(); // create a new object 
     $mail->IsSMTP(); // enable SMTP 
     $mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only 
     $mail->SMTPAuth = true; // authentication enabled 
     $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail 
     $mail->SMTPOptions = array(
      'ssl' => array(
       'verify_peer' => false, 
       'verify_peer_name' => false, 
       'allow_self_signed' => true 
      ) 
     ); 
     //$mail->Host = $host; 
     $mail->Host = "smtp.gmail.com"; 
     $mail->Port = 587; // or 465 
     $mail->IsHTML(true); 
     $mail->Username = $username; 
     $mail->Password = $password; 
     $mail->SetFrom($username); 

     $mail->Subject = $subject; 
     $mail->Body =$body; 
     $mail->AddAddress($to); 
     $mail->Send(); 
     echo 'Message has been sent'; 
    } catch (Exception $e) { 
     echo 'Message could not be sent.'; 
     echo 'Mailer Error: ' . $mail->ErrorInfo; 
    } 
} 
} 
Verwandte Themen