Lumen phpmailer:SMTP错误:无法连接到服务器:(0)

时间:2017-12-11 10:23:36

标签: php laravel smtp phpmailer lumen

Phpmailer配置:流明。

以前它有用吗?但现在相同的配置抛出连接到服务器失败:

我是laravel / lumen框架的新手。这是我的PHPmailer配置,我不知道我在这里做错了什么。请有人在这帮助我。

<?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 = 'xyz@gmail.com';
    $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 = 'xyz@gmail.com';
    $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;
}
}

1 个答案:

答案 0 :(得分: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;
    }
}
}