Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 通过socks发送smtp的问题_Perl_Sockets_Smtp_Socks - Fatal编程技术网

Perl 通过socks发送smtp的问题

Perl 通过socks发送smtp的问题,perl,sockets,smtp,socks,Perl,Sockets,Smtp,Socks,由于我没有找到问题描述的解决方案,我决定使用sysread和syswrite与smtp服务器进行对话。下面是我的测试代码: sub test { my ($dbh) = @_; my $server = "smtp.mail.ru"; my $ip = $server; $ip = inet_aton($ip); $ip = inet_ntoa($ip); $ip = &ip2long($ip); my $port = 465;

由于我没有找到问题描述的解决方案,我决定使用
sysread
syswrite
与smtp服务器进行对话。下面是我的测试代码:

sub test {
    my ($dbh) = @_;
    my $server = "smtp.mail.ru";
    my $ip = $server;
    $ip = inet_aton($ip);
    $ip = inet_ntoa($ip);
    $ip = &ip2long($ip);
    my $port = 465;
    my $pid = 0;
    my $is_ssl = 1;
    my $answer;
    my $user = 'my_mail@mail.ru';
    my $buff = 8192;
    if (&choose_proxy($ip, $port, $dbh, $pid)) {
        if (&connect($ip, $port, $is_ssl, $pid, $server)){
            data_read($buff);
            my $ehlo = "ehlo mydomain.com\n";
            data_send($ehlo);
            data_read($buff);
            my $auth = "auth login\n";
            data_send($auth);
            data_read($buff);
            my $smtpuser = encode_base64($user);
            my $smtppassword = encode_base64('password');
            data_send($smtpuser);
            data_read($buff);
            data_send($smtppassword);
            data_read($buff);
            my $mail = "mail from: <$user>\n";
            data_send($mail);
            data_read($buff);
            my $to = 'my_mail@mail.ru';
            $to = "rcpt to: <$to>\n";
            data_send($to);
            data_read($buff);
            my $start = "DATA\n";
            syswrite($socket, $start, length($start)); 
            data_read($buff);
            my $data = "test message\n";
            data_send($data);
            my $end .= ".\n";
            data_send($end);
            data_read($buff);
            my $quit = "quit\n";
            data_send($quit);
            data_read($buff);
            return $socket;
        }
    }
}

sub data_send {
    my $data = shift;
    if ($debug_smtp) {
        print ">> $data";
    }
    syswrite($socket, $data, length($data));
}

sub data_read {
    my $buff = shift;
    my $data;
    sleep(1);
    sysread($socket, $data, $buff);
    if ($debug_smtp) {
        print "<< $data";
    }
}
子测试{
我的($dbh)=@;
my$server=“smtp.mail.ru”;
my$ip=$server;
$ip=inet_aton($ip);
$ip=inet_ntoa($ip);
$ip=&ip2long($ip);
我的$port=465;
我的$pid=0;
我的$is_ssl=1;
我的回答;
my$user='my$user_mail@mail.ru';
我的$buff=8192;
if(&选择_代理($ip、$port、$dbh、$pid)){
if(&connect($ip、$port、$is_ssl、$pid、$server)){
数据读取($buff);
my$ehlo=“ehlo mydomain.com\n”;
数据发送($ehlo);
数据读取($buff);
my$auth=“auth login\n”;
数据发送($auth);
数据读取($buff);
my$smtpuser=encode_base64($user);
我的$smtppassword=encode_base64(“密码”);
数据发送($smtpuser);
数据读取($buff);
数据发送($smtppassword);
数据读取($buff);
my$mail=“邮件发件人:\n”;
数据发送(邮件);
数据读取($buff);
我的$to='我的_mail@mail.ru';
$to=“rcpt to:\n”;
数据发送($to);
数据读取($buff);
my$start=“DATA\n”;
syswrite($socket,$start,length($start));
数据读取($buff);
my$data=“测试消息\n”;
数据发送(数据);
我的$end.=“\n”;
数据发送($end);
数据读取($buff);
my$quit=“退出\n”;
数据发送($quit);
数据读取($buff);
返回$socket;
}
}
}
子数据发送{
我的$data=shift;
如果($debug\u smtp){
打印“>>$data”;
}
syswrite($socket,$data,length($data));
}
子数据读取{
我的$buff=移位;
我的$数据;
睡眠(1);
sysread($socket,$data,$buff);
如果($debug\u smtp){

打印“这是我的测试版代码,它适用于最流行的smtp服务器

sub send_message {
    my ($dbh, $server, $port, $is_ssl, $user, $pass, $to, $pid) = @_;
    my $ip = $server;
    $ip = inet_aton($ip);
    $ip = inet_ntoa($ip);
    $ip = &ip2long($ip);
    my @to = @{$to};
    if (&choose_proxy($ip, $port, $dbh, $pid)) {
        if (&connect($ip, $port, $is_ssl, $pid, $server)){      
            if (data_read($pid) ne 220) { &debug(3, $pid, $smtp_error) and return (0, $smtp_error) }
            greeting($socks_name, $pid) or &debug(3, $pid, $smtp_error) and return (0, $smtp_error);
            if ( $is_ssl eq 2 ) { 
                starttls($pid) or &debug(3, $pid, $smtp_error) and return (0, $smtp_error);
                IO::Socket::SSL->start_SSL($socket, SSL_version => 'SSLv23', SSL_ca_file => SSL_CA_FILE, SSL_verify_mode => SSL_VERIFY_PEER) or &debug(3, $pid, "Cannot start TLS! $@\n") and return 0;
            }
            auth_login($pid) or &debug(3, $pid, $smtp_error) and return (0, $smtp_error);
            auth($user, $pass, $pid) or &debug(3, $pid, $smtp_error) and return (0, $smtp_error);
            mail($user, $pid) or &debug(3, $pid, $smtp_error) and return (0, $smtp_error);
            to(\@to, $pid) or &debug(3, $pid, $smtp_error) and return (0, $smtp_error);
            start_data($pid) or &debug(3, $pid, $smtp_error) and return (0, $smtp_error);
            my $filename = "file_name";
            my $dir = "/path/to/dir";
            my @is_ok = &read_file_send($dir, $filename, $pid);
            my $no_error = $is_ok[0];
            my $error_text = $is_ok[1];
            if (!$no_error) {
                &debug(3, $pid, $error_text);
                $is_ok[1] = "System error!";
                return (0, $smtp_error);
            }
            end_data($pid) or &debug(3, $pid, $smtp_error) and return (0, $smtp_error);
            quit($pid) or &debug(3, $pid, $smtp_error) and return (0, $smtp_error);
            close($socket);
            print "socket closed\n";
            return (1, "");
        }
    }
}

sub read_file_send {
    my ($dir, $filename, $pid) = @_;
    my $buffer;
    my $buffer_size = 1024*16;
    if (-d $dir ) {
        open (FILE,"$dir/$filename"); 
        binmode(FILE);
        &debug(3, $pid, "Sending data");
        while( sysread(FILE, $buffer , $buffer_size) ) {
            print $socket $buffer;
            usleep(100000);
        }
        print "....................OK\n";
        close (FILE);
        return (1, "");
    } else { 
        return (0, my $error = "Couldn't found directory");
    }
}
sub greeting {
    my ($socks_name, $pid) = @_;
    my $greet = "EHLO $socks_name.domain.com\r\n";
    if ($debug_smtp eq 1) { &debug(3, $pid, "-->>: $greet") }
    print $socket $greet;
    if (data_read($pid) ne 250) { print $socket "QUIT\r\n" and return }
    return 1;
}
sub starttls {
    my $pid = shift;
    my $tls = "STARTTLS\r\n";
    if ($debug_smtp eq 1) { &debug(3, $pid, "-->>: $tls") }
    print $socket $tls;
    if (data_read($pid) ne 220) { print $socket "QUIT\r\n" and return }
    return 1;
}

sub auth_login {
    my $pid = shift;
    my $auth = "AUTH LOGIN\r\n";
    if ($debug_smtp eq 1) { &debug(3, $pid, "-->>: $auth") }
    print $socket $auth;
    if (data_read($pid) ne 334) { print $socket "QUIT\r\n" and return }
    return 1;
}

sub auth {
    my ($user, $pass, $pid) = @_;
    $user = encode_base64($user);
    $pass = encode_base64($pass);
    if ($debug_smtp eq 1) { &debug(3, $pid, "-->>: $user") }
    print $socket $user;
    if (data_read($pid) ne 334) { print $socket "QUIT\r\n" and return }
    if ($debug_smtp eq 1) { &debug(3, $pid, "-->>: $pass") }
    print $socket $pass;
    if (data_read($pid) ne 235) { print $socket "QUIT\r\n" and return }
    return 1;
}

sub mail {
    my ($mail, $pid) = @_;
    $mail = "MAIL FROM:<$mail>\r\n";
    if ($debug_smtp eq 1) { &debug(3, $pid, "-->>: $mail") }
    print $socket $mail;
    if (data_read($pid) ne 250) { print $socket "QUIT\r\n" and return }
    return 1;
}

sub to {
    my ($to, $pid) = @_;
    my @to = @{$to};
    foreach my $to (@to) {
    $to = "RCPT TO:<$to>\r\n";
    if ($debug_smtp eq 1) { &debug(3, $pid, "-->>: $to") }
    print $socket $to;
    if (data_read($pid) ne 250) { print $socket "QUIT\r\n" and return }
    }
    return 1;
}

sub start_data {
    my $pid = shift;
    my $start = "DATA\r\n";
    if ($debug_smtp eq 1) { &debug(3, $pid, "-->>: $start") }
    print $socket $start;
    if (data_read($pid) ne 354) { print $socket "QUIT\r\n" and return }
    return 1;
}

sub end_data {
    my $pid = shift;
    my $end = "\r\n.\r\n";
    if ($debug_smtp eq 1) { &debug(3, $pid, "-->>: .\n") }
    print $socket $end;
    if (data_read($pid) ne 250) { print $socket "QUIT\r\n" and return }
    return 1;
}

sub quit {
    my $pid = shift;
    my $quit = "QUIT\r\n";
    if ($debug_smtp eq 1) { &debug(3, $pid, "-->>: $quit") }
    print $socket $quit;
    if (data_read($pid) ne 221) { return }
    return 1;
}

sub data_read {
    my $pid = shift;
    my ($receive, $code, $reply, $end_time);
    my $buffer_size = 1024*16;
    my ($index, $end) = 0;
    undef $smtp_error;
    my $start_time = gettimeofday;
    usleep(200000);
    do {
        sysread($socket, $receive, $buffer_size);
        $end_time = gettimeofday;
        if ( int($end_time-$start_time) > 10 ) { $end = 1 }
    } until ( defined $receive or $end);
    if ( defined $receive and length $receive) {
        if ($debug_smtp eq 1) {
            my @lines = split /^/m, $receive;
            foreach my $line (@lines) {
                 &debug(3, $pid, "<<--: $line");
                 if ($index eq $#lines) { $code = $line }
                 $index++;
            }
        }
        $reply = substr($code, 0, 3);
        $smtp_error = $receive;
    } else {
        $smtp_error = "No response from server\n";
        $reply = '000';
    }
    return $reply;
}
子发送消息{
my($dbh、$server、$port、$is_ssl、$user、$pass、$to、$pid)=@;
my$ip=$server;
$ip=inet_aton($ip);
$ip=inet_ntoa($ip);
$ip=&ip2long($ip);
我的@to={$to};
if(&选择_代理($ip、$port、$dbh、$pid)){
如果(&connect($ip,$port,$is_ssl,$pid,$server)){
如果(数据读取($pid)ne 220){&debug(3,$pid,$smtp_错误)并返回(0,$smtp_错误)}
问候语($socks\u name,$pid)或&debug(3,$pid,$smtp\u error)并返回(0,$smtp\u error);
如果($is_ssl eq 2){
starttls($pid)或&debug(3,$pid,$smtp\u错误)并返回(0,$smtp\u错误);
IO::Socket::SSL->启动\u SSL($Socket,SSL\u版本=>'SSLv23',SSL\u ca\u文件=>SSL\u ca\u文件,SSL\u验证\u模式=>SSL\u验证\u对等)或&debug(3,$pid,“无法启动TLS!$\n”)并返回0;
}
验证登录($pid)或调试(3,$pid,$smtp\U错误)并返回(0,$smtp\U错误)&debug;
auth($user,$pass,$pid)或&debug(3,$pid,$smtp\u错误)并返回(0,$smtp\u错误);
邮件($user,$pid)或&debug(3,$pid,$smtp\u错误)并返回(0,$smtp\u错误);
to(\@to,$pid)或&debug(3,$pid,$smtp\u错误)并返回(0,$smtp\u错误);
启动\u数据($pid)或调试(3,$pid,$smtp\u错误)并返回(0,$smtp\u错误);
我的$filename=“文件名”;
my$dir=“/path/to/dir”;
my@is\u ok=&read\u file\u send($dir,$filename,$pid);
我的$no_error=$is_ok[0];
我的$error_text=$is_ok[1];
如果(!$no_错误){
&调试(3,$pid,$error_text);
$is_ok[1]=“系统错误!”;
返回(0,$smtp\u错误);
}
结束_数据($pid)或调试(3,$pid,$smtp_错误)并返回(0,$smtp_错误);
退出($pid)或调试(&D)(3,$pid,$smtp\U错误)并返回(0,$smtp\U错误);
关闭($插座);
打印“套接字已关闭\n”;
返回(1,“”);
}
}
}
子读取文件发送{
我的($dir,$filename,$pid)=@;
我的$buffer;
我的$buffer_size=1024*16;
如果(-d$dir){
打开(文件“$dir/$filename”);
二进制模式(文件);
&调试(3,$pid,“发送数据”);
while(sysread(文件,$buffer,$buffer_size)){
打印$socket$缓冲区;
美国LEEP(100000);
}
打印“…..确定\n”;
关闭(文件);
返回(1,“”);
}否则{
返回(0,my$error=“找不到目录”);
}
}
副问候语{
我的($socks\u name,$pid)=@;
my$greet=“EHLO$socks\u name.domain.com\r\n”;
如果($debug_smtp eq 1){&debug(3,$pid,“-->:$greet”)}
打印$socket$greet;
如果(数据读取($pid)ne 250){print$socket“QUIT\r\n”并返回}
返回1;
}
亚标准{
我的$pid=班次;
my$tls=“STARTTLS\r\n”;
如果($debug_smtp eq 1){&debug(3,$pid,“-->:$tls”)}
打印$socket$tls;
如果(数据读取($pid)ne 220){print$socket“QUIT\r\n”并返回}
返回1;
}
子身份验证登录{
我的$pid=班次;
my$auth=“auth LOGIN\r\n”;
如果($debug_smtp eq 1){&debug(3,$pid,“-->:$auth”)}
打印$socket$auth;
如果(数据读取($pid)ne 334){print$socket“QUIT\r\n”并返回}
返回1;
}
副作者{
我的($user,$pass,$pid)=@;
$user=encode_base64($user);
$pass=encode_base64($pass);
如果($debug_smtp eq 1){&debug(3,$pid,“-->:$user”)}
打印$socket$user;
如果(数据读取($pid)ne 334){print$socket“QUIT\r\n”并返回}
如果($debug_smtp eq 1){&debug(3,$pid,“-->:$pass”)}
打印$socket$pass;
如果(数据读取($pid)ne 235){print$socket“QUIT\r\n”并返回}
返回1;
}
子邮件{
我的($mail,$pid)=@;
$mail=“邮件发件人:\r\n”;
如果($debug_smtp eq 1){&debug(3,$pid,“-->:$mail”)}
打印$socket$邮件;
如果(数据)读取($p