问题
I would like to use Net::SMTP with dynamic socks proxy. IO::Socket::Socks is aware of socks but how it should be used with net::smtp?
回答1:
I figured it out, but it includes hack that may or may not work with future version of Net::SMTP :
use Net::SMTP;
use Net::SOCKS;
my $socks = new Net::SOCKS(socks_addr=>$shost,socks_port=>$sport, protocol_version=>5) or die $!;
my $socksfd = $socks->connect(peer_addr=>$smtp_server,peer_port=>25);
if(!$socksfd){
die "Connection to SOCKS failed";
}
my $smtp = Net::SMTP->new_from_fd($socksfd->fileno, 'r+' ) or die $!;
#HACK: there is "220 host.domain.net" line we must read otherwise Net::SMTP would not work!
$smtp->getline();
$smtp->hello("localhost") or die $smtp->message();
#from here Net::SMTP business as usual...
来源:https://stackoverflow.com/questions/3253360/using-socks5-proxy-with-netsmtp