SMTP connect() failed PHPmailer - PHP

2022-08-30 15:30:08

我是PHP的新手。我试图通过PHPmailer给自己发送一封示例电子邮件。我正在使用 gmail 的 smtp 服务器。我正在尝试将示例邮件从我的 gmail 帐户发送到我的雅虎帐户。但是我得到错误:
这是代码:Mailer Error: SMTP connect() failed.

<?php

require "class.phpmailer.php";
$mail = new PHPMailer(); 
$mail->IsSMTP();                              // send via SMTP
$mail->Host = "ssl://smtp.gmail.com";
$mail->SMTPAuth = true;                       // turn on SMTP authentication
$mail->Username = "myemail@gmail.com";        // SMTP username
$mail->Password = "mypassword";               // SMTP password
$webmaster_email = "myemail@gmail.com";       //Reply to this email ID
$email="myyahoomail@yahoo.in";                // Recipients email ID
$name="My Name";                              // Recipient's name
$mail->From = $webmaster_email;
$mail->Port = 465;
$mail->FromName = "My Name";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"My Name");
$mail->WordWrap = 50;                         // set word wrap
$mail->IsHTML(true);                          // send as HTML
$mail->Subject = "subject";
$mail->Body = "Hi,
This is the HTML BODY ";                      //HTML Body 
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body 

if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>

我在Windows 7 64位计算机上使用WAMP服务器。可能是什么?
请帮我解决这个问题。谢谢!


答案 1

这个问题的解决方案真的非常简单。实际上,Google开始为其用户使用新的授权机制。您可能已经在调试控制台中看到另一行提示您使用任何浏览器登录您的帐户。这是因为谷歌自2014年开始使用新的XOAUTH2身份验证机制。记得。。不要在端口 465 上使用 ssl,而是在 587 上使用 tls。这仅仅是因为XOAUTH2身份验证机制。如果您使用超过 465 的 ssl,您的请求将被退回。

你真正需要做的是..登录到您的谷歌帐户,并打开以下地址 https://www.google.com/settings/security/lesssecureapps 并选中打开。您必须这样做才能让您与Google SMTP连接,因为根据新的身份验证机制,Google会从所有不遵循任何标准加密技术的应用程序反弹所有请求。检查后打开..你很好去..这是对我来说工作正常的代码。

require_once 'C:\xampp\htdocs\email\vendor\autoload.php';

define ('GUSER','youremail@gmail.com');
define ('GPWD','your password');


// make a separate file and include this file in that. call this function in that file.

function smtpmailer($to, $from, $from_name, $subject, $body) { 
    global $error;
    $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->SMTPAutoTLS = false;
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;

    $mail->Username = GUSER;  
    $mail->Password = GPWD;           
    $mail->SetFrom($from, $from_name);
    $mail->Subject = $subject;
    $mail->Body = $body;
    $mail->AddAddress($to);
    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Message sent!';
        return true;
    }
}

答案 2

您需要添加参数Host

$mail->Host = "ssl://smtp.gmail.com"; 

另外,请检查您是否已启用。open_ssl

<?php
echo !extension_loaded('openssl')?"Not Available":"Available";

推荐