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

时间:2015-07-09 11:56:58

标签: php email

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

3 个答案:

答案 0 :(得分:0)

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

在本地尝试相同的脚本,它应该可以工作。

答案 1 :(得分:0)

<强>的变化:

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

   ......

尝试这些更改,它有效。一切顺利.. :))

答案 2 :(得分:0)

1-将端口更改为587

 $Mail->Port= 587;

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

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

试试。