3
<?php
set_time_limit(0); 
ignore_user_abort(true);
ini_set('max_execution_time', 0);


// 207.46.10.10:993 hotmail imap server 

$s = "\r\n";

$proxy = '13.89.36.103'; // proxy
$port =53;

$fp = fsockopen($proxy, $port);
//socket_set_timeout($fp, 10, 0);

fputs($fp, "CONNECT 207.46.10.10:993 HTTP/1.1".$s.$s);
fputs($fp, "n1 login user@hotmail.com userpassword".$s);
fputs($fp, "n2 select Inbox".$s);
fputs($fp, "n3 UID FETCH 100289 BODY[]".$s);
fputs($fp, "n4 LOGOUT".$s.$s);

while(!feof($fp)){
    $line = fgets($fp, 4000);
    echo $line."\n";
}
fclose($fp);
?>

Is it possible to use a proxy connection to read mail from a imap server using PHP's

it work fine without proxy
please if someone can help me i don't want to use curl

  • You'll need to start a new SSL communication layer after CONNECT. Also, are you running an HTTP proxy on port 53? – Max Jul 02 '16 at 13:51
  • You also seem to have the proxy port specified twice. – Max Jul 02 '16 at 13:52
  • @Max this is not my proxy it's just public proxy ,you can give more details about ssl layer after CONNECT, im sure i don't need it because im using proxy and not direct connection to imap server – aziz mouhoud Jul 02 '16 at 14:04
  • Port 993 is secure imap: the proxy won't do security for you. After you send connect and the 200 response back, you then need to negotiate SSL yourself. Incidentally, this is how https works over proxies: this was the proxy can't snoop your data. – Max Jul 02 '16 at 16:44
  • @Max i think u are right i test it in smtp server ant it work great, the problem is the ssl layer in imap connection i should figure out how to make proxy do ssl connection to imap server. – aziz mouhoud Jul 02 '16 at 18:49
  • The proxy won't, that is your job :). You'll need to wrap your socket with SSL. – Max Jul 03 '16 at 16:04
  • when i do it with proxy it give me just (HTTP/1.1 200 Connection established) but when i connect without proxy it give me (OK Outlook.com IMAP4rev1 server version 17.4.0.0 ready (BLU451-IMAP318) ) so i need that meesage to show when i use the proxy – aziz mouhoud Jul 04 '16 at 17:50
  • Yes, after 200, it is waiting for you to begin TLS negotiations. – Max Jul 05 '16 at 12:53
  • @Max the proxy should begin the negotiations it's not me because in that moment the proxy ho had the controlle(if you have any idea what should i change in the code thx) – aziz mouhoud Jul 11 '16 at 10:11
  • @Max or you can tell me how to begin the TLS negotations – aziz mouhoud Jul 11 '16 at 14:36
  • The proxy does not do TLS for you: that would be a security issue. When you use the CONNECT verb, it is just providing a dumb pipe. I dot not know the PHP libraries, which is why I didn't provide an answer, but surely you can search for SSL/TLS socket wrappers. – Max Jul 11 '16 at 15:35
  • Check this question: http://stackoverflow.com/questions/5265692/smtp-server-response-530-5-7-0-must-issue-a-starttls-command-first One of the answers uses stream_socket_enable_crypto to turn on SSL on an already connected socket. – Max Jul 11 '16 at 15:37

2 Answers2

2
<?php
set_time_limit(0); 
ignore_user_abort(true);
ini_set('max_execution_time', 0);
$s = "\r\n";
$socket = stream_socket_client("tcp://128.199.105.86:3128", $errno, $errstr, 30);

if($socket) {

    echo "con";
    $buf = null;
    $buff = null;

    fwrite($socket, "CONNECT imap-mail.outlook.com:993 HTTP/1.1\r\n");
    fwrite($socket,"Host: imap-mail.outlook.com:993\r\n");
    fwrite($socket, "Proxy-Connection: Keep-Alive\r\n\r\n");

    while(!feof($socket)){  
    $buf .= fgets($socket, 4096);  
    if(preg_match('/^http/i', $buf)){

        break;

    } 
}  

        $modes = array( 
    STREAM_CRYPTO_METHOD_TLS_CLIENT, 
    STREAM_CRYPTO_METHOD_SSLv3_CLIENT, 
    STREAM_CRYPTO_METHOD_SSLv23_CLIENT, 
    STREAM_CRYPTO_METHOD_SSLv2_CLIENT 
); 


$success = false; 
foreach($modes as $mode) { 
    $success = stream_socket_enable_crypto($socket, true, $mode); 
    if ($success) {
        echo "done";
        break;
    } 
} 

 while(!feof($socket)){  
    $buf .= fgets($socket, 4096);
    if(preg_match('/^\* ok/i', $buf)){

        break;

    } 
}  
echo $buf;


    fclose($socket);



}
?>

the working version

0

The HTTP proxy provides a dumb pipe after using the CONNECT verb. Since you are connecting to a secure IMAP service, you need to enable SSL/TLS after the connection has been established.

For some searching around, it looks like you can use stream_socket_enable_crypto after you get a 200 response from the PROXY server. It is very likely you have to read the response before using stream_socket_enable_crypto, or it will throw an error when it reads the unexpected data.

Max
  • 10,701
  • 2
  • 24
  • 48
  • im so close to the solution know i test stream_socket_enable_crypto but i seems not easy thanx for your help if i found somthing i tell you – aziz mouhoud Jul 11 '16 at 17:50