简体   繁体   中英

getting SMTP ERROR: Failed to connect to server

I am using phpmailer for sending a mail. But When I submit the application, this is what i am getting SMTP -> ERROR: Failed to connect to server: Connection refused (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.

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

 $Mail->Port= 587;

2- change your Account Security setting

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

And Try .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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