简体   繁体   English

收到SMTP错误:无法连接到服务器

[英]getting SMTP ERROR: Failed to connect to server

I am using phpmailer for sending a mail. 我正在使用phpmailer发送邮件。 But When I submit the application, this is what i am getting SMTP -> ERROR: Failed to connect to server: Connection refused (111) 但是,当我提交应用程序时,这就是我得到的SMTP->错误:无法连接到服务器:连接被拒绝(111)

<?php 
    require 'classes/class.phpmailer.php';
    require 'classes/class.smtp.php';
    $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       = "mail.yourdomain.com"; // SMTP server
      $mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
      $mail->SMTPAuth   = true;                  // enable SMTP authentication
      $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
      $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
      $mail->Port       = 587;                   // set the SMTP port for the GMAIL server
      $mail->Username   = "test2gmail.com";  // GMAIL username
      $mail->Password   = "test123";            // GMAIL password
      $mail->AddAddress('test@gail.com', 'name');
      $mail->SetFrom('test@gmail.com', 'test');
      $mail->AddReplyTo('test@gmail.com', 'testing');
      $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
      $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
      $mail->MsgHTML(file_get_contents('contents.html'));
     // $mail->AddAttachment('images/phpmailer.gif');      // attachment
     // $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment

      $mail->Send();

      echo "Message Sent OK</p>\n";
    } catch (phpmailerException $e) {
      echo $e->errorMessage(); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
      echo $e->getMessage(); //Boring error messages from anything else!
    }
    ?>

Either your server is not allowing you to use that port, or gmail server is not allowing a connection from your server. 您的服务器不允许您使用该端口,或者gmail服务器不允许您的服务器进行连接。

Try the same script locally, it should work. 在本地尝试相同的脚本,它应该可以工作。

Changes: 变化:

$mail = new PHPMailer; 

try {
  // remove comment and add your mailing server
  $mail->Host = "mail.yourdomain.com";

  // if not working then change "ssl" to "tls"
  $mail->SMTPSecure = "tls";          

  // change port 465 to 25 in case if not work
  $mail->Port = 465;      

   ......

Try these change, it works. 尝试这些更改,它可以工作。 All the best.. :) 祝一切顺利.. :)

1- Change Port to 587 1-将端口更改为587

 $Mail->Port= 587;

2- change your Account Security setting 2-更改您的帐户安全设置

https://www.google.com/settings/security/lesssecureapps

And Try . 并尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM