收到SMTP错误:连接到服务器失败


getting SMTP ERROR: Failed to connect to server

我正在使用phpmailer发送邮件。但当我提交申请时,这就是我得到的SMTP ->错误: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!
    }
    ?>

您的服务器不允许您使用该端口,或者gmail服务器不允许从您的服务器连接。

变化:

$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;      
   ......

尝试这些改变,它工作。愿一切都好!:)

1-将Port改为587

 $Mail->Port= 587;

2-更改您的帐户安全设置

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

And Try .