SMTP connect()失败PHPmailer - PHP

Chi*_*bke 16 php email

我是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";
}
?>
Run Code Online (Sandbox Code Playgroud)

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

小智 18

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

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

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 多谢。+100 为你的答案。我已经完成了所有 Gmail 设置并花了一整天的时间,但只有在关闭不太安全的应用程序选项后才开始工作 (2认同)

Sha*_*ran 12

您需要添加Host参数

$mail->Host = "ssl://smtp.gmail.com"; 
Run Code Online (Sandbox Code Playgroud)

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

<?php
echo !extension_loaded('openssl')?"Not Available":"Available";
Run Code Online (Sandbox Code Playgroud)


小智 6

通过将这些行添加到标准PHPMailer配置中,解决了几乎相同的问题。奇迹般有效。

$mail->SMTPKeepAlive = true;   
$mail->Mailer = “smtp”; // don't change the quotes!
Run Code Online (Sandbox Code Playgroud)

在此研究解决方案时浏览了这段代码(来自Simon Chen),https://webolio.wordpress.com/2008/03/02/phpmailer-and-smtp-on-1and1-shared-hosting/#comment-89

  • 不要这样做,这使它根本无法与 SMTP 一起使用,它只是由您的主机发送,这就是为什么您会产生它正在工作的错觉 (4认同)

Jho*_*tan 5

故障排除

您已添加此代码:

 $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );
Run Code Online (Sandbox Code Playgroud)

并启用允许不太安全的应用程序:“通常会解决 PHPMailer 的问题,并且它不会真正使您的应用程序安全性显着降低。据报道,更改此设置可能需要一个小时或更长时间才能生效,因此不要指望立即使固定”

这对我有用!