使用CURL通过代理发送电子邮件

时间:2013-03-06 12:25:51

标签: php email curl smtp fsockopen

我想在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 CURL CLASS

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.0EHLO提供帮助?这是不可能的!

2为什么第一个例子适用于fsockopen,但是那个带有'curl'的女孩我认为它更强大并且具有代理功能以及对象和类?

3为什么这个ProxySmtp类适用于任何其他服务器?

4为什么我可以在cmd.exe(RAW样式)中复制上面的所有命令,我可以发送电子邮件?

5使用“\ r \ n”来说telnet“执行该行”是否正确?

6是否可以“代理”第一个脚本? (使fsockopen通过代理)

HINT 根据我的理解,你不能在这样的行中向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协议。

0 个答案:

没有答案