Connecting to a SMTP server with fsockopen from behing a proxy with PHP
I'm trying to connect to a external SMTP server from behind a proxy. I figured out how to let fsockopen get a website from behind a proxy, but not how to connect to a SMTP server. The SMTP server requires a TLS connection and username and password authentication.
What I have for getting a webpage from behind a proxy is the following code.
<?
$proxy_fp = fsockopen('111.111.111.111', '80');
if (!$proxy_fp) {
return false;
}
fputs($proxy_fp, "GET http://wwww.google.com HTTP/1.0\r\nHost: 111.111.111.111\r\n\r\n");
while (!feof($proxy_fp)) {
$proxy_cont .= fread($proxy_fp, 4096);
}
fclose($proxy_fp);
$proxy_cont = substr($proxy_cont, strpos($proxy_cont, "\r\n\r\n") + 4);
return $proxy_cont;
?>
Now how do I let the proxy connect to the SMTP server?
--- EDIT ---<?
$s = "\r\n";
$proxy = '24.47.131.253';
$port = 8123;
$fp = fsockopen($proxy, $port);
socket_set_timeout($fp, 10, 0);
fputs($fp, "CONNECT smtp.domain.com:25 HTTP/1.0".$s.开发者_高级运维$s);
while(!feof($fp)){
$line = fgets($fp, 4000);
echo $line."\n";
}
fclose($fp);
?>
I now have this based on the answers. The problem is that I don't get a response from the SMTP server, it just prints empty lines. Could someone point me in the right direction?
SMTP and HTTP aren't mutually compatible protocols; in fact, they are completely different from each other.
What you could try is to use a loophole in the HTTP CONNECT method: instead of GET or POST, send CONNECT yourserver:yourport HTTP/1.0
- you should get a TCP connection to the requested host and port (see this for a more complete reference). Note that some proxies will limit the allowed destination port to 443 (HTTPS) or otherwise inspect the CONNECT traffic, to prevent people from tunneling to any service (e.g. SMTP).
Note that in most organizations, such actions might be interpreted as "circumventing access controls and/or security policy", so proceed with caution.
精彩评论