SMTP连接失败 [英] SMTP connection failed

查看:318
本文介绍了SMTP连接失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此脚本运行良好之前,我从过去的三天开始就遇到这个问题.现在出现错误:

I'm facing this issue from last three days, before this script worked perfectly. Now getting error:

SMTP错误:无法连接到服务器:(0)2017-10-06 21:05:34 SMTP connect()失败. https://github.com/PHPMailer/PHPMailer/wiki/疑难解答消息未收到Mailer错误:SMTP connect()失败. https://github.com/PHPMailer/PHPMailer/wiki/疑难解答 ahsanazhar12@gmail.com

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 ahsanazhar12@gmail.com

这是我的剧本:

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 = 'example@gmail.com'; // SMTP username
$mail->Password = 'mypassword';        // SMTP password
$mail->SMTPSecure = 'tls';             // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;   
$mail->setFrom('example@gmail.com', 'Your Name');
$mail->addAddress('example@gmail.com', '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或类似方式发送电子邮件的设置.

I think Gmail may have changed settings for sending emails using SMTP, or something like that.

推荐答案

最后,我能够从本地主机发送电子邮件.这是我的代码.

Finally i'm able to send emails from localhost. Here is my code.

要安装:

  1. 下载PHPMailer
  2. 将其添加到您的项目中(我将其放在根目录上)
  3. 将自动加载类添加到脚本中.
  4. 其余代码低于

  1. Download PHPMailer
  2. Add it to your project (i put it on root)
  3. Add Autoload class to your Script.
  4. Rest of code is below

        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 = 'example@gmail.com';                 // 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('example@gmail.com', "Mailer");
        $mail->addAddress("example@gmail.com","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>"));
            }

这篇关于SMTP连接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆