PHP & SSL: Connection Refused

SuperDave2U

New member
Aug 13, 2008
134
6
0
Got my app to work with a non-ssl box but when tested on a server with SSL I'm getting connection refused.

I've added an ssl:// prefex to the server address and port 995 with no luck. Anyone able to point me in the right direction to manage SSL?

I've double checked my server settings:

  • --with-imap-ssl=/opt/openssl
  • --with-openssl=/opt/openssl
  • --with-openssl-dir=/opt/openssl
  • Registered Stream Socket Transports: tcp, udp, unix, udg, ssl, sslv3, sslv2, tls
  • IMAP c-Client Version: 2006k, SSL Support :enabled
  • OpenSSL: Enabled, Version: OpenSSL 0.9.8i 15 Sep 2008

Anyone able to reproduce this error with my code?
PHP:
$pop3 = new POP3;
$pop3->server = "ssl://pop3.domain.com";
$pop3->port="995";
$pop3->user = "someaddress@domain.com";
$pop3->passwd = "password";
$pop3->debug = TRUE; 

if($pop3->pop3_connect()) {
    $pop3->pop3_login();
    $pop3->pop3_stat();
    for ($i=0,$j=$pop3->mailboxno;$i<=$j;$i++) {
     if($pop3->pop3_retr($i)) {
       while($line = $pop3->nextAnswer())
           $msg_body[] = $line;
       $mContent = join($msg_body);
       echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n\n";
        echo "This message was downloaded :  \n\t". date("r") ."\n";
        echo "From Account : \n\t" . $pop3->user ."\n";
        echo "\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n" ;
        echo $mContent;
       }   
    }   
    $pop3->pop3_disconnect();
}
PHP:
class POP3 {
    var $server;          // POP 3 Server Address
    var $port = 110;    // POP 3 Server Port
    var $sockfd;          // So cket Descriptor
    var $user; // POP 3 Mailbox User ID
    var $passwd; // POP 3 Mailbox User Password
    var $answer; // POP 3 Server Answer
    var $mailsize; // Mail size
    var $mailno; // Mail number
    var $mailboxno; // Number of mails in mailbox
    var $mailboxsize; // How many bytes are you using in you mailbox
    var $debug = FALSE; // Debug Class

    function cmdOK() {
        $this->answer = fgets($this->sockfd, 2048);
        if($this->debug) echo "\nS: $this->answer";
        if(ereg("^\+OK", $this->answer))
        return true;
        else
        return false;
    }

    function sendCmd($cmd, $param1 = '', $param2 = '') {
        if($this->debug) echo "\nC:$cmd $param1 $param2";
        fwrite($this->sockfd, trim("$cmd $param1 $param2") . "\r\n");
    }

    function nextAnswer() {
        $line = fgets($this->sockfd, 2048);
        return (ereg("^\.\r\n$", $line)?false:$line);
    }

    function pop3_connect($block = 'true') {
        if($this->sockfd = fsockopen($this->server, $this->port, $errno, $errstr)) {
            if($this->cmdOK()) {
                return true;
            }
        }
        return false;
    }

    function pop3_disconnect() {
        fwrite($this->sockfd, "QUIT\r\n");
        $this->cmdOK();
        fclose($this->sockfd);
    }

    function pop3_login() {
        $this->sendCmd("USER", $this->user);
            if($this->cmdOK()) {
                $this->sendCmd("PASS", $this->passwd);
            if($this->cmdOK()) {
                return true;
            }
        }
        return false;
    }

    function pop3_stat() {
        $this->sendCmd("STAT");
        $state = $this->cmdOK();
        if(ereg("^\+OK ([0-9]+) ([0-9]+)", $this->answer, $aux)) {
            $this->mailboxno = $aux[1];
            $this->mailboxsize = $aux[2];
        }
        return $state;
    }

    function pop3_list($mailno = '') {
        $this->sendCmd("LIST", $mailno);
        $state = $this->cmdOK();
        if(ereg("^\+OK ([0-9]+) ([0-9]+)", $this->answer, $aux)) {
            $this->mailno = $aux[1];
            $this->mailsize = $aux[2];
        }
        return $state;
    }

    function pop3_top($mailno = 1, $lines = 0) {
        $this->sendCmd("TOP", $mailno, $lines);
        return $this->cmdOK();
    }

    function pop3_retr($mailno = 1) {
        $this->sendCmd("RETR", $mailno);
        return $this->cmdOK();
    }

    function pop3_dele($mailno = 1) {
        $this->sendCmd("DELE", $mailno);
        return $this->cmdOK();
    }

    function pop3_rset() {
        $this->sendCmd("RSET");
        return $this->cmdOK();
    }

    function pop3_noop() {
        $this->sendCmd("NOOP");
        return $this->cmdOK();
    }

    function pop3_uidl($mailno = '') {
        $this->sendCmd("UIDL", $mailno);
        return $this->cmdOK();
    }
 


Does the server you are connecting to support SSL and is it properly configured? Some of them only work right if you connect with TLS.
 
Is the server gmail/google apps? This is what helped me get phplist to check gmail via pop3; notice the port setting. I had to remove the ssl:// from my server string and make the port string look like below.

$bounce_mailbox_host = 'pop.gmail.com';
$bounce_mailbox_user = 'username@gmail.com';
$bounce_mailbox_password = '********';
$bounce_mailbox_port = "995/pop3/ssl/novalidate-cert" ;