PHP 测试 SMTP 配置

问题描述 投票:0回答:2

使用 PHPMailer http://phpmailer.worxware.com/index.php?pg=examples 我如何在不实际发送电子邮件的情况下测试我的 SMTP 设置是否正常工作?

编辑:也许我应该让它更通用,只问我如何使用 PHP 检查 SMTP 连接,而不发送电子邮件。

php email smtp phpmailer
2个回答
3
投票

如果您使用的是 PHPMailer,您可以使用 Connect() 方法,在发送之前,如果连接不正确则检查错误。

  /**
  * Connect to the server specified on the port specified.
  * If the port is not specified use the default SMTP_PORT.
  * If tval is specified then a connection will try and be
  * established with the server for that number of seconds.
  * If tval is not specified the default is 30 seconds to
  * try on the connection.
  *
  * SMTP CODE SUCCESS: 220
  * SMTP CODE FAILURE: 421
  * @access public
  * @return bool
  */
  public function Connect($host, $port = 0, $tval = 30) {
    // set the error val to null so there is no confusion
    $this->error = null;

    // make sure we are __not__ connected
    if($this->connected()) {
      // already connected, generate error
      $this->error = array("error" => "Already connected to a server");
      return false;
    }

    if(empty($port)) {
      $port = $this->SMTP_PORT;
    }

    // connect to the smtp server
    $this->smtp_conn = @fsockopen($host,    // the host of the server
                                  $port,    // the port to use
                                  $errno,   // error number if any
                                  $errstr,  // error message if any
                                  $tval);   // give up after ? secs
    // verify we connected properly
    if(empty($this->smtp_conn)) {
      $this->error = array("error" => "Failed to connect to server",
                           "errno" => $errno,
                           "errstr" => $errstr);
      if($this->do_debug >= 1) {
        echo "SMTP -> ERROR: " . $this->error["error"] . ": $errstr ($errno)" . $this->CRLF . '<br />';
      }
      return false;
    }

    // SMTP server can take longer to respond, give longer timeout for first read
    // Windows does not have support for this timeout function
    if(substr(PHP_OS, 0, 3) != "WIN")
      socket_set_timeout($this->smtp_conn, $tval, 0);

    // get any announcement
    $announce = $this->get_lines();

    if($this->do_debug >= 2) {
      echo "SMTP -> FROM SERVER:" . $announce . $this->CRLF . '<br />';
    }

    return true;
  }

(来自PHPMailer SVN


0
投票

您可以使用这个简单的脚本: https://gist.github.com/froemken/447580af285d96de4ad09aa54745a774

您不需要凭据,此脚本测试 smtp 连接。我使用它,它帮助我向主机证明服务器设置阻止 smtp 工作。

它显示调试的有用信息并生成日志文件。

你不需要

PHPMailer

© www.soinside.com 2019 - 2024. All rights reserved.