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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lumen phpmailer:SMTP错误:无法连接到服务器:相关的知识,希望对你有一定的参考价值。

phpmailer配置:流明。

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

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

<?php

namespace AppRepositories;
use AppRepositoriesBaseRepository;
use AppModelsForgetModel;
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
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) . '
';
    $headers .= 'MIME-Version: 1.0
';
    $headers .= 'Content-Type: text/html; charset=ISO-8859-1
';

    $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. run the command  "composer require phpmailer/phpmailer" in your cmd.



namespace AppRepositories;
use AppRepositoriesBaseRepository;
use AppModelsForgetModel;
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;

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;
    }
}
}

以上是关于Lumen phpmailer:SMTP错误:无法连接到服务器:的主要内容,如果未能解决你的问题,请参考以下文章

PHPMailer无法连接到SMTP主机

为啥 PHPMailer 无法连接到我的 SMTP 服务器

PHPMailer:SMTP错误:10051无法连接到服务器:尝试对无法访问的网络执行套接字操作

PHPmailer:SMTP 连接()失败(不工作)

PHPMailer 使用 SMTP 身份验证发送电子邮件,但 Gmail 仍然声称它无法验证发件人

带有 mandrill smtp 的 PHPMailer 给出连接错误