【问题标题】:SMTP connect() failed PHPmailer - PHPSMTP 连接()失败 PHPmailer - PHP
【发布时间】:2014-05-20 14:15:41
【问题描述】:

我是 PHP 新手。我试图通过 PHPmailer 给自己发送一封示例电子邮件。我正在使用 gmail 的 smtp 服务器。我正在尝试从我的 gmail 帐户向我的 yahoo 帐户发送示例邮件。但我收到错误消息: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 服务器。可能是什么问题?
请帮我解决这个问题。谢谢!

【问题讨论】:

  • 只是让您知道,require 是一种语言结构,() 不是必需的。
  • @RPM 谢谢!我改变了它。但是,请帮我解决这个问题
  • 我认为你错过了服务器详细信息
  • @Krishna 请告诉我应该添加哪些细节。
  • 代码中的主机参数在哪里?

标签: php email


【解决方案1】:

需要添加Host参数

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

另外,检查您是否启用了open_ssl

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

【讨论】:

  • 我添加了参数,但它显示相同的错误Mailer Error: SMTP connect() failed.
  • 机器连接到互联网了吗?
  • @Krishna 是的,我的电脑已连接到互联网
  • 好吧,这就是为什么它不起作用,打开你的php.ini并搜索这一行;extension=php_openssl.dll并删除它前面的分号,保存文件并重新启动你的网络服务器。进行此更改后,您将看到您的编码正常工作。
  • 我只是想感谢@ShankarDamodaran 的回答。它救了我!谢谢
【解决方案2】:

您缺少说明连接使用 SSL 的指令

require ("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); 
$mail->SMTPAuth = true;     // turn of SMTP authentication
$mail->Username = "YAHOO ACCOUNT";  // SMTP username
$mail->Password = "YAHOO ACCOUNT PASSWORD"; // SMTP password
$mail->SMTPSecure = "ssl";
$mail->Host = "YAHOO HOST"; // SMTP host
$mail->Port = 465;

然后添加其他部分

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

附带说明一下,我在同时使用 Body + AltBody 时遇到了麻烦,尽管它们应该可以工作。结果,我编写了以下完美运行的包装函数。

<?php
require ("class.phpmailer.php");

// Setup Configuration for Mail Server Settings
$email['host']          = 'smtp.email.com';
$email['port']          = 366;
$email['user']          = 'from@email.com';
$email['pass']          = 'from password';
$email['from']          = 'From Name';
$email['reply']         = 'replyto@email.com';
$email['replyname']     = 'Reply To Name';

$addresses_to_mail_to = 'email1@email.com;email2@email.com';
$email_subject = 'My Subject';
$email_body = '<html>Code Here</html>';
$who_is_receiving_name = 'John Smith';

$result = sendmail(
    $email_body,
    $email_subject,
    $addresses_to_mail_to,
    $who_is_receiving_name
);

var_export($result);


function sendmail($body, $subject, $to, $name, $attach = "") {

  global $email;
  $return = false;

  $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
  $mail->IsSMTP(); // telling the class to use SMTP
  try {
    $mail->Host       = $email['host']; // SMTP server
//    $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Host       = $email['host']; // sets the SMTP server
    $mail->Port       = $email['port'];                    // set the SMTP port for the GMAIL server
    $mail->SMTPSecure = "tls";
    $mail->Username   = $email['user']; // SMTP account username
    $mail->Password   = $email['pass'];        // SMTP account password
    $mail->AddReplyTo($email['reply'], $email['replyname']);
    if(stristr($to,';')) {
      $totmp = explode(';',$to);
      foreach($totmp as $destto) {
        if(trim($destto) != "") {
          $mail->AddAddress(trim($destto), $name);
        }
      }
    } else {
      $mail->AddAddress($to, $name);
    }
    $mail->SetFrom($email['user'], $email['from']);
    $mail->Subject = $subject;
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
    $mail->MsgHTML($body);
    if(is_array($attach)) {
        foreach($attach as $attach_f) {
            if($attach_f != "") {
              $mail->AddAttachment($attach_f);      // attachment
            }
        }
    } else {
        if($attach != "") {
          $mail->AddAttachment($attach);      // attachment
        }
    }
    $mail->Send();
  } catch (phpmailerException $e) {
    $return = $e->errorMessage();
  } catch (Exception $e) {
    $return = $e->errorMessage();
  }

  return $return;
}

【讨论】:

  • 我不明白你的回答。我正在尝试从我的 gmail 帐户向我的 yahoo 帐户发送电子邮件,反之亦然。如果我错了,请纠正我,但我应该提供我的 gmail 帐户的用户名和密码,而不是我的 yahoo 帐户。
  • 正确,您应该输入您的 GMail 帐户凭据。给我一点时间,我会更新这个功能,这样你就可以使用了:)
  • 谢谢萨努埃尔。我现在开始工作了。 +1 帮助我。我对这个功能感兴趣。请更新。
  • 功能已更新。它也允许附加文件,但您必须使用完整路径附加。 $attach 可以设置为一个值(又名 $attach = '/home/somefile.txt')或者它可以是一个文件数组( $attach = Array( '/home/somefile1.txt', '/home/somefile2 .txt')).
  • 我唯一的怪癖是,由于某种原因,如果我向函数添加参数(又名 $ssl),我不能将该值用作 $mail-&gt;SMTPSecure 的值(又名, $ssl = "tls"; 然后$mail-&gt;SMTPSecure = $ssl 不行……所以如果需要切换,直接设置那行就好了。
【解决方案3】:

这个问题的解决方法真的很简单。实际上,Google 开始为其用户使用新的授权机制。您可能已经在调试控制台中看到另一行提示您使用任何浏览器登录您的帐户。!这是因为谷歌从 2014 年开始使用新的 XOAUTH2 身份验证机制。 记住.. 不要使用 ssl over 465 端口,而是使用 tls over 587。这只是因为 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;
    }
}

【讨论】:

  • 非常感谢。 +100 为您的答案。我已经完成了所有 gmail 设置并度过了一整天,但只有在我关闭不太安全的应用程序选项后才开始工作
  • 应用程序打开了,我用 465 和 ssl 做的另一件事,我还没有遇到 gmail 的任何反弹。
  • 我爱你“不要在端口 465 上使用 ssl,而是在 587 上使用 tls”。我失去了这么多小时......
  • 感谢您在这里找到了您的贡献
【解决方案4】:

如果一切都失败了,那么对于 gmail,您必须打开对 3rd 方应用程序的访问权限才能连接到您的 gmail 帐户。

https://www.google.com/settings/security/lesssecureapps // 转动它 开

【讨论】:

    【解决方案5】:

    如果有人仍然无法解决问题,请检查以下线程并关注 callmebob 的回答。

    PHPMailer - SMTP ERROR: Password command failed when send mail from my server

    【讨论】:

      【解决方案6】:

      Troubleshooting

      您已添加此代码:

       $mail->SMTPOptions = array(
              'ssl' => array(
                  'verify_peer' => false,
                  'verify_peer_name' => false,
                  'allow_self_signed' => true
              )
          );
      

      并启用Allow less secure apps: “通常会解决 PHPMailer 的问题,而且它并不会真正降低您的应用程序的安全性。据报道,更改此设置可能需要一个小时或更长时间才能生效,所以不要指望立即修复”

      这对我有用!

      【讨论】:

        【解决方案7】:

        通过将这些行添加到标准 PHPMailer 配置中,解决了几乎相同的问题。像魅力一样工作。

        $mail->SMTPKeepAlive = true;   
        $mail->Mailer = “smtp”; // don't change the quotes!
        

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

        【讨论】:

        • 看起来这个命令解决了这个问题;主要是只需要第二个命令。有趣的是,我的 gmail 帐户将收到的电子邮件过滤为垃圾邮件。
        • 这应该没问题。但这不是一个适当的解决方案。收到的电子邮件被标记为垃圾邮件。最好使用 PHP 框架通过 SMTP 发送电子邮件。
        • 不要这样做,这使得它根本无法与 SMTP 一起使用,它只是由您的主机发送的,这就是为什么您会产生它工作的错觉
        【解决方案8】:

        如果您使用 VPS 和 httpd 服务,请检查您的 httpd_can_sendmail 是否开启。

        getsebool -a | grep mail
        

        开启

        setsebool -P httpd_can_sendmail on
        

        【讨论】:

          【解决方案9】:

          我修好了...

          https://github.com/PHPMailer/PHPMailer/tree/5.2-stable

          <?php
          require 'PHPMailerAutoload.php';
          
          $mail = new PHPMailer;
          
          //$mail->SMTPDebug = 3;                               // 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 = 'm7@gmail.com';                 // SMTP username
          $mail->Password = 'pass';                           // SMTP password
          //$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
          $mail->Port = 25;                                    // TCP port to connect to
          
          $mail->setFrom('m7@gmail.com', 'Mailer');
          $mail->addAddress('dot@gmail.com', 'User');     // Add a recipient
          
          $mail->isHTML(true);                                  // Set email format to HTML
          
          $mail->Subject = 'Here is the subject';
          $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
          $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
          
          if(!$mail->send()) {
              echo 'Message could not be sent.';
              echo 'Mailer Error: ' . $mail->ErrorInfo;
          } else {
              echo 'Message has been sent';
          }

          打开访问并享受..!那是在Gmail帐户设置上。

          【讨论】:

            【解决方案10】:

            尝试将此行添加到您的脚本中。这对我有用!

            $mail-&gt;Mailer = “smtp”;

            【讨论】:

            • 不要这样做,这使得它根本无法与 SMTP 一起使用,它只是由您的主机发送的,这就是为什么您会产生它工作的错觉
            【解决方案11】:

            这通常是因为服务器不接受 SSLv2 或 SSLv3 连接,这是 cPanel/WHM 的标准,支持仅 TLS 连接。您可以通过 WHM>>Service Configuration>>Exim Configuration Manager -> Options for OpenSSL 来检查这一点

            【讨论】:

              【解决方案12】:
               $mail->SMTPOptions = array(
                      'ssl' => array(
                          'verify_peer' => false,
                          'verify_peer_name' => false,
                          'allow_self_signed' => true
                      )
                  );
              

              【讨论】:

              【解决方案13】:
              <?php
              
              use PHPMailer\PHPMailer\PHPMailer;
              
              require_once "PHPMailer/src/Exception.php";
              require_once "PHPMailer/src/PHPMailer.php";
              require_once "PHPMailer/src/SMTP.php";
              
              
              
              $mail = new PHPMailer();
              
              $mail ->isSMTP();
              $mail ->Host ="smtp.gmail.com";
              $mail -> SMTPAuth = true;
              $mail ->Username = 'youremail@gmail.com';
              $mail ->Password = 'password';
              //$mail ->SMTPSecure=PHPMailer::ENCRYPTION_STARTTLS;
              $mail ->Port = '587';
              $mail ->SMTPSecure ='tls';
              $mail ->isHTML(true);
              
              $mail ->setFrom('youremail@gmail.com','email send name');
              
              $mail ->addAddress('receiver@gmail.com');
              $mail ->Subject = 'HelloWorld';
              $mail ->Body = 'a test email';
              
              
              $mail->SMTPOptions = array(
                  'ssl' => array(
                      'verify_peer' => false,
                      'verify_peer_name' => false,
                      'allow_self_signed' => true
                  )
              );
              
              
              
              if ($mail->send()){
                  echo "Message has been sent successfully";
              }else{
                  echo "Mailer Error: " . $mail->ErrorInfo;
              }
              
              ?>
              

              确保您使用此链接https://www.google.com/settings/security/lesssecureapps 在 gmail 中配置不太安全的应用程序设置,并确保您从 github 下载了 PHPMailer-master.zip

              在本地使用 MAMP 服务器正常工作 :)

              【讨论】:

                【解决方案14】:

                只要确保你传递了正确的参数 例如。正确的传出服务器主机、用户名和密码

                $mail->SMTPDebug = false;
                $mail->Host = 'email_smtp_host'
                $mail->SMTPAuth = false
                $mail->Username = 'username'
                $mail->Password = 'password'
                $mail->SMTPSecure 'tls'
                $mail->Port = '587'
                

                【讨论】:

                  【解决方案15】:
                  $mail->SMTPKeepAlive = true;   
                  $mail->isSendMail(); // instead of  isSMTP();
                  

                  在共享主机上,您的邮件服务器可能偶尔会遇到连接问题。 Here I found solution

                  [编辑]

                  这是链接内容,以防链接因某种原因停止工作(发生)

                  PHPMailerSMTP1and1 共享主机上

                  我在我的 1and1 共享主机帐户上使用 PHPMailer。最近,我不能再发送电子邮件了。

                  我尝试调试,这是PHPMailer 抛出的错误:

                  语言字符串加载失败:connect_host

                  在谷歌搜索解决方案并尝试不同的 SMTP 服务器、帐户和 SMTP 端口后,我决定切换到 sendmail,它的效果非常棒!我需要做的就是将$mail-&gt;isSmtp() 替换为:

                  $mail->isSendMail()
                  

                  sendMail 位于 1and1 服务器上的默认位置:/usr/sbin/sendmail,因此无需更改设置。

                  结论:

                  1and1 可能在其共享托管服务器上关闭了其传出 SMTP 端口。因此,如果您使用PHPMailer,请不要再使用 SMTP 模式。

                  【讨论】:

                    【解决方案16】:

                    经过几天的搜索,我发现协议名称需要小写。

                    $mail->SMTPSecure = "ssl"; # lowercase
                    

                    【讨论】:

                      猜你喜欢
                      • 2014-06-13
                      • 2015-08-09
                      • 2016-11-10
                      • 1970-01-01
                      • 1970-01-01
                      • 2015-09-25
                      • 2021-01-30
                      • 1970-01-01
                      • 2016-07-14
                      相关资源
                      最近更新 更多