PEAR邮件连接拒绝smtp.gmail.com


PEAR Mail connection refused to smtp.gmail.com

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

我在本地机器上使用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

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

"连接ssl失败://smtp.gmail.com:465 [SMTP:连接socket失败:Connection refused (code: -1, response:)]"

我还尝试了端口587和443,但没有成功。我猜问题一定在于socket.php, smtp.php, mail.php甚至服务器配置文件,因为上面的代码似乎没有什么问题。如果有人能给我指出正确的方向,我会非常感激!

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

请求主机支持php_openssl。或者您可以手动尝试以下方法来加载dll。

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

这是一个普遍错误,因为extension=php_openssl.dll文件没有在服务器上取消注释。

Google拒绝连接,因为您需要在"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文档将其列为可选…