SMTP 连接失败 - Localhost 上的 PHPMailer 错误

SMTP connect failed - PHPMailer Error on Localhost

我使用的是最新版本的 PHPMailer,我能够使用它发送电子邮件。但是现在我不知道发生了什么事?现在我无法使用它发送邮件。它给了我以下错误 -

Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

我也检查了 openssl 扩展。

这是我的代码 -

<form method="post" action="mail.php">
  <input type="text" placeholder="Enter your email to subscribe" name="emailid" />
  <input type="submit" value="Submit" />
</form>

mail.php 文件的内容

<?php
$emailSubscr = $_POST['emailid'];
$sub = 'New Subscriber';
$message = $emailSubscr.' has subscribed for newsletter';

require 'phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;

$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';     // SMTP Host Name
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'myusername@gmail.com';                 // SMTP username
$mail->Password = 'mypassword';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->From = $emailSubscr;
$mail->FromName = 'Subscriber';
$mail->addAddress('me@example.com', 'Rohit Kumar');     // Add a recipient
$mail->addReplyTo($emailSubscr, 'Subscriber');

$mail->Subject = $sub;
$mail->Body    = $message;

if(!$mail->send()) {
    echo 'Some error occured, please try again later.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Thanks for subscribing our newsletter on your email id.';
}
?>

谢谢 Faiz Rasool。我的问题已解决。

can you do this please $mail->SMTPDebug = 3; and post the log   – Faiz Rasool

其实我用的是Mobile Broadband,ip经常变。昨天,当我尝试使用我的应用程序访问它时,google 可能检测到我是可疑登录。
当我登录到我的 gmail 帐户时,我看到了更改密码的通知,因为我的应用程序是无法登录我的帐户。
你帮助了我。当我检查调试日志时,有一条来自 google 的消息说要通过网络浏览器登录。我这样做了,重置了密码,选择了可疑的登录尝试作为我,我的问题就解决了.. 但这里最主要的是,在设置 google 的选项 - Allow less secure apps 为 ON 之后,Google 有时仍然阻止我的应用程序。我认为这背后的原因(我认为)是当我断开并重新连接移动宽带时我的 ip 发生了变化。因此,我的位置发生了变化。 顺便说一句,我不再为我的应用程序使用 GMail 帐户。