【问题标题】:PEAR Mail connection refused to smtp.gmail.comPEAR 邮件连接拒绝 smtp.gmail.com
【发布时间】:2020-01-17 14:06:09
【问题描述】:

我在尝试使用 pear mail 包发送邮件时遇到了无穷无尽的麻烦:

我在本地机器上使用 xampp 进行测试,以下代码完美运行:

//PEAR

    require_once('../PEAR/Mail.php');


    $from = "<sender@domain.com>";
    $to = "<receiver@domain.com>";
    $subject = "Hi";
    $body = "Testing message";

    $host = "ssl://smtp.gmail.com"; //ssl://
    $port = "465";
    $username = "my_account@gmail.com";
    $password = "**********";

    $headers = array ('From' => $from,
      'To' => $to,
      'Subject' => $subject);
    $smtp = Mail::factory('smtp',
      array ('host' => $host,
        'port' => $port,
        'auth' => true,
        'username' => $username,
        'password' => $password));

    $mail = $smtp->send($to, $headers, $body);

    function &factory($driver, $params = array())
{
    $driver = strtolower($driver);
    @include_once 'Mail/' . $driver . '.php';
    $class = 'Mail_' . $driver;
    if (class_exists($class)) {
        $mailer = new $class($params);
        return $mailer;
    } else {
        return PEAR::raiseError('Unable to find class for driver ' . $driver);
    }
}


   if (PEAR::isError($mail)) {
      echo("<p>" . $mail->getMessage() . "</p>");
     } else {
      echo("<p>Message successfully sent!</p>");
     }

    //end of php tag

但是,当我将文件上传到在线网络服务器并运行完全相同的脚本时,我收到以下错误:

“无法连接到 ssl://smtp.gmail.com:465 [SMTP:连接套接字失败:连接被拒绝(代码:-1,响应:)]”

我也尝试了端口 587 和 443 无济于事。我猜问题一定出在 socket.php、smtp.php、mail.php 甚至服务器配置文件上,因为上面的代码似乎没有任何问题。如果有人能指出我正确的方向,我将不胜感激!

【问题讨论】:

  • 为什么你的主机 URL 上有ssl://
  • 连接拒绝某些东西主动拒绝允许连接。可能是谷歌,也可能是沿线某处的防火墙—​​—但确实有东西发回了一个数据包,上面写着“没办法,何塞”。它不太可能是您的代码。可能您的 ISP 不允许连接到他们自己网络之外的邮件服务器(宽带 ISP 倾向于这样做作为反垃圾邮件措施)。

标签: php gmail pear


【解决方案1】:

是的,因为您的主机可能不支持 ssl 连接。

请向您的主机寻求 php_openssl 支持。或者您可以手动尝试以下加载dll。

如果(dl(php_openssl.so)) 或者 if(dl(php_openssl.dll))

这是一般错误,因为 extension=php_openssl.dll 文件未在服务器上取消注释。

【讨论】:

    【解决方案2】:

    连接被谷歌拒绝,因为您需要在“Mail::factory”调用中指定“localhost”参数,如下代码所示:

    $host = "ssl://smtp.gmail.com"; //ssl://
    $port = "465";
    $username = "my_account@gmail.com";
    $password = "**********";
    $localhostname = gethostname();
    $headers = array ('From' => $from,
      'To' => $to,
      'Subject' => $subject);
    $smtp = Mail::factory('smtp',
      array ('host' => $host,
        'port' => $port,
        'auth' => true,
        'localhost' => $localhostname,
        'username' => $username,
        'password' => $password));
    

    那么电子邮件应该可以正常通过了!我遇到过不需要提供“localhost”参数的托管服务提供商,但对于其他服务提供商来说,即使官方 PEAR 文档将其列为可选参数,它似乎也是必需的......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      • 2016-02-21
      • 2017-03-13
      • 2019-02-05
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多