PEAR Mail connection refused to smtp.gmail.com
I'm having endless trouble while trying to send mail using the pear mail package:
I'm using xampp on my local machine for testing purposes and the following code works perfectly:
//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
HOWEVER, when I upload the file to the online web server and run the exact same script I receive the following error:
"Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]"
I have also tried ports 587 and 443 to no avail. I'm guessing the problem must lie with either socket.php, smtp.php, mail.php or even with the server config files since there seems to be nothing wrong with the above code. I would be extremely grateful if someon开发者_JS百科e could point me in the right direction!
Ya because your host may not support ssl connection.
Ask your host for php_openssl support. Or either you can manually try the following to load dll.
if(dl(php_openssl.so)) or if(dl(php_openssl.dll))
this is general error because of extension=php_openssl.dll file is not uncommented on server.
Connection is refused by Google because you need to specify the "localhost" parameter in "Mail::factory" call as shown in the code below:
$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));
Then the email should go through just fine! I have encountered hosting providers where it wasn't necessary to provide the "localhost" parameter but with others it seems to be required even though the official PEAR documentation lists it as optional...
精彩评论