Search code examples
perlsmtpsocks

Using socks5 proxy with Net::SMTP


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?


Solution

  • 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...