Ben Reisner
Ben Reisner

Reputation: 662

PHP Pear Mail SSL Trouble Connecting

I am receiving

Failed to connect to ssl://smtp.gmail.com:587 [SMTP: Failed to connect socket: (code: -1, response: )]

when I try to send out SMTP email. I've tried both ssl:// and tls:// with no success.

I've verified that my computer can make such an outgoing connection by using outlook configured with an SMTP account and it was able to succesfully send out with TLS.

I also verified that OpenSSL is enabled on the webserver with phpinfo(). It shows PHP 5.3.15 and there is an openssl section that shows OpenSSL Support: enabled

Relevent code:

if($isSSL)
    $host="tls://$host";

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

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

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

any thoughts?

Upvotes: 1

Views: 10014

Answers (1)

cweiske
cweiske

Reputation: 31088

ssl:// should work. Please also try port 465.

Use $mail->getUserInfo() to get a more detailled error message.

Another problem source could be that your firewall doesn't allow outgoing connections to that port.

Upvotes: 2

Related Questions