我想用 PHP 编写一个脚本,通过代理连接到 smtp 服务器上的 TELNET。到目前为止,我所拥有的是这两种变体:
1 FSOCKOPEN(我无法代理此连接)
$connection = fsockopen("smtp.comcast.net",25,$errno,$errstr,10);
if(!$connection)
{
echo "Connection error";
}
else
{
$command = "HELO smtp.comcast.net\r\n";
fwrite($connection,$command);
while (!feof($connection))
{
echo fgets($connection, 128)."\n";
}
$command = "AUTH LOGIN\r\n";
fwrite($connection,$command);
while (!feof($connection))
{
echo fgets($connection, 128)."\n";
}
$command = base64_encode("username")."\r\n";
fwrite($connection,$command);
while (!feof($connection))
{
echo fgets($connection, 128)."\n";
}
$command = base64_encode("password")."\r\n";
fwrite($connection,$command);
while (!feof($connection))
{
echo fgets($connection, 128)."\n";
}
$command = "QUIT\r\n";
fwrite($connection,$command);
while (!feof($connection))
{
echo fgets($connection, 128)."\n";
}
}
但它正在输出我:
220 omta01.emeryville.ca.mail.comcast.net comcast ESMTP server ready
250 omta01.emeryville.ca.mail.comcast.net hello [xx.xx.xx.xx], pleased to meet you
334 VXNlcm5hbWU6
女巫一切都好
和另一个变种
2 卷曲类
class ProxySmtp
{
private $server;
private $port;
private $proxy;
private $timeout;
private $curl_connection;
public function __construct($server,$port,$timeout = 10,$proxy = NULL)
{
echo "SERVER: $server:$port \n";
echo "PROXY: $proxy \n";
echo "TIMEOUT: $timeout \n";
// Set the config
$this->server = $server;
$this->port = $port;
$this->timeout = $timeout;
$this->proxy = $proxy;
// Start the curl
$this->curl_connection = curl_init($this->server.":".$this->port);
var_dump($this->curl_connection);
}
// Setters
public function setServer($newserver)
{
$this->server = $newserver;
}
public function setPort($newport)
{
$this->port = $newport;
}
public function setProxy($newproxy)
{
$this->proxy = $newproxy;
}
public function setTimeout($newtimeout)
{
$this->timeout = $newtimeout;
}
// Getters
public function getServer()
{
echo $this->server;
return $this->server;
}
public function curl_telnet($query)
{
if (!function_exists('curl_init'))
{
user_error('"curl_init()" function does not exist.', E_USER_WARNING);
return false;
}
curl_setopt($this->curl_connection, CURLOPT_TIMEOUT,$this->timeout);
curl_setopt($this->curl_connection, CURLOPT_CONNECTTIMEOUT,$this->timeout);
curl_setopt($this->curl_connection, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($this->curl_connection, CURLOPT_CUSTOMREQUEST,$query);
if($this->proxy != NULL)
{
echo "USING PROXY : ".$this->proxy."\n";
curl_setopt($this->curl_connection,CURLOPT_PROXY,$this->proxy);
curl_setopt($this->curl_connection,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);
}
$output=curl_exec($this->curl_connection);
//curl_close($this->curl_connection);
if($output == false)
{
echo "CONNECTION ERROR!\n";
}
return $output;
}
public function __destruct()
{
$Destroy = curl_close($this->curl_connection);
echo "Object destroyed (".$Destroy.")! \n";
}
}
// Server,port,timeout,proxy(optional)
$ProxyAndPort = $Proxy.":".$ProxyPort;
$Smtp = new ProxySmtp($Server,$Port,$Timeout,$ProxyAndPort);
var_dump($Smtp->getServer());
var_dump($Smtp->curl_telnet("EHLO $User \r\n"));
var_dump($Smtp->curl_telnet("AUTH LOGIN \r\n"));
var_dump($Smtp->curl_telnet(base64_encode($user)." \r\n"));
var_dump($Smtp->curl_telnet(base64_encode($pass)." \r\n"));
var_dump($Smtp->curl_telnet("QUIT \r\n"));
女巫给了我
string(389) "220 omta12.westchester.pa.mail.comcast.net comcast ESMTP server ready
250-omta12.westchester.pa.mail.comcast.net hello [85.204.11.249], pleased to meet you
250-HELP
250-AUTH LOGIN PLAIN
250-SIZE 36700160
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-STARTTLS
250 OK
500 5.5.1 command unrecognized
500 5.5.1 command unrecognized
450 4.7.0 too many invalid commands (closing session)
....
....
我有 6 个问题非常困扰我:
1 为什么它给了我一个简单的500 5.5.1
命令?这是不可能的!450 4.7.0
EHLO
2 为什么第一个示例适用于fsockopen
,但我认为带有“curl”女巫的那个更强大并且具有代理功能以及对象和类?
3 为什么这个 ProxySmtp 类可以在任何其他服务器上正常工作?
4 为什么我可以在 cmd.exe(RAW 样式)中复制上面的所有命令并且我可以发送电子邮件?
5 用“\r\n”对telnet说“执行线路”对吗?
6 是否可以“代理”第一个脚本?(使 fsockopen 到通过代理)
提示 据我所知,您不能在这样的一行中向 COMCAST 发送命令:
$command = "HELO smtp.comcast.net\r\n";
$command .= "AUTH LOGIN\r\n";
$command .= base64_encode("username")."\r\n";
$command .= base64_encode("password")."\r\n";
$command .= "QUIT\r\n";
fwrite($connection,$command);
while (!feof($connection))
{
echo fgets($connection, 128)."\n";
}
你必须这样做(LINE,SENT,LINE,SENT ...):
$command = "HELO smtp.comcast.net\r\n";
fwrite($connection,$command);
while (!feof($connection))
{
echo fgets($connection, 128)."\n";
}
$command = "AUTH LOGIN\r\n";
fwrite($connection,$command);
while (!feof($connection))
{
echo fgets($connection, 128)."\n";
}
$command = base64_encode("username")."\r\n";
fwrite($connection,$command);
while (!feof($connection))
{
echo fgets($connection, 128)."\n";
}
$command = base64_encode("password")."\r\n";
fwrite($connection,$command);
while (!feof($connection))
{
echo fgets($connection, 128)."\n";
}
$command = "QUIT\r\n";
fwrite($connection,$command);
while (!feof($connection))
{
echo fgets($connection, 128)."\n";
}
并且似乎 CURL 不想这样做。
我真的无法理解这是怎么回事,并且需要使用 PHP 类,因为我应该使用类来构建我的 WebMail 服务器管理员,这是第一部分,我还必须进入 IMAP 和 POP3 协议也是。