S
S
strongmonkey2015-12-23 17:13:21
Perl
strongmonkey, 2015-12-23 17:13:21

How to send email using perl?

Help me to understand. There is a send_mail function (parameters here)
on the smtp server NTLM authorization
code itself

#! /usr/bin/perl -w

sub send_mail {
    use Net::SMTP;
  
    my ($smtpserver, $to, $from, $msg, $subject, $apass) = @_;
    my $error;
    my $body;
    my $smtp;
    
    $body = "MIME-Version: 1.0\n"
        . "From: $from\n"
        . "To: $to \n"
        . "Subject: $subject\n\n" 
        . $msg . "\n"; 
 
  eval 
  {
    use Authen::SASL;
    use MIME::Base64;
    };
  
  if ([email protected])
  {
    $error = system("ppm install Authen::SASL");
    $error = system("ppm install MIME::Base64");
  };
  
    if (!ref($to))
    { 
        @$to = split(/[,;]\s*/, $to); 
    }
    if(!($smtp = Net::SMTP->new($smtpserver, Debug => 1)))
    {
        return "Could not connect to SMTP Server: $smtpserver \n";
    }
  if (length($apass) > 0)
  {
    if(!($smtp->auth(encode_base64('login'), encode_base64('pass'))))
    {
      return $smtp->message(); 
    }
  }
    if (!($smtp->mail($from)))
    {
        return "Bad From, check your Ac count Information " . $smtp->message();
    }
    if (!($smtp->recipient(@$to)))
    {	
        return "Bad EmailTo " . $smtp->message();
    }
    
    $smtp->data($body);
    $smtp->quit;
    
    return 0;
}

debug log
Process started >>>
Net::SMTP>>> Net::SMTP(2.34)
Net::SMTP>>> Net::Cmd(2.30)
Net::SMTP>>> Exporter(5.71)
Net::SMTP>> > IO::Socket::INET(1.35)
Net::SMTP>>> IO::Socket(1.38)
Net::SMTP>>> IO::Handle(1.35)
Net::SMTP=GLOB(0x34f62e8)<< < 220 srvexcas2.******.local Microsoft ESMTP MAIL Service ready at Wed, 23 Dec 2015 05:51:47 -0800
Net::SMTP=GLOB(0x34f62e8)>>> EHLO localhost.localdomain
Net::SMTP =GLOB(0x34f62e8)<<< 250-srvexcas2.******.local Hello [172.20.14.75]
Net::SMTP=GLOB(0x34f62e8)<<< 250-SIZE 37748736
Net::SMTP=GLOB(0x34f62e8 )<<< 250-PIPELINING
Net::SMTP=GLOB(0x34f62e8)<<<250-DSN
Net::SMTP=GLOB(0x34f62e8)<<< 250-ENHANCEDSTATUSCODES
Net::SMTP=GLOB(0x34f62e8)<<< 250-STARTTLS
Net::SMTP=GLOB(0x34f62e8)<<< 250-X-ANONYMOUSTLS
Net::SMTP=GLOB(0x34f62e8)<<< 250-AUTH NTLM
Net: :SMTP=GLOB(0x34f62e8)<<< 250-X-EXPS GSSAPI NTLM
Net::SMTP=GLOB(0x34f62e8)<<< 250-8BITMIME
Net::SMTP=GLOB(0x34f62e8)<<< 250-BINARYMIME
Net:: SMTP=GLOB(0x34f62e8)<<< 250-CHUNKING
Net::SMTP=GLOB(0x34f62e8)<<< 250 XRDST
Net::SMTP=GLOB(0x34f62e8)>>> AUTH NTLM
Net::SMTP=GLOB(0x34f62e8)< << 334 NTLM supported
Net::SMTP=GLOB(0x34f62e8)>>>
Net::SMTP=GLOB(0x34f62e8)<<< 535 5.7.3 Authentication unsuccessful
<<< Process finished.(Exit code 1)
=============== READY ================

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2015-12-23
@strongmonkey

Read the doc for NTLM authorization. Your client does not send a Type 1 message, so nothing happens until the timeout.
Try with Net::SMTP_auth , Authen::NTLM may also be needed

A
Andrey, 2015-12-23
@VladimirAndreev

login/password incorrect

S
strongmonkey, 2015-12-24
@strongmonkey

Net::SMTP_auth followed your advice using Net::SMTP_auth. The log has changed, like the password was sent to the server. But the error is the same.

Process started >>>
Net::SMTP_auth>>> Net::SMTP_auth(0.08)
Net::SMTP_auth>>> Net::SMTP(2.34)
Net::SMTP_auth>>> Net::Cmd(2.30)
Net:: SMTP_auth>>> Exporter(5.71)
Net::SMTP_auth>>> IO::Socket::INET(1.35)
Net::SMTP_auth>>> IO::Socket(1.38)
Net::SMTP_auth>>> IO::Handle (1.35)
Net::SMTP_auth=GLOB(0x32a7210)<<< 220 srvexcas2.bops.local Microsoft ESMTP MAIL Service ready at Wed, 23 Dec 2015 22:04:43 -0800
Net::SMTP_auth=GLOB(0x32a7210)>> > EHLO localhost.localdomain
Net::SMTP_auth=GLOB(0x32a7210)<<< 250-srvexcas2.bops.local Hello [172.20.14.75]
Net::SMTP_auth=GLOB(0x32a7210)<<<250-SIZE 37748736
Net::SMTP_auth=GLOB(0x32a7210)<<< 250-PIPELINING
Net::SMTP_auth=GLOB(0x32a7210)<<< 250-DSN
Net::SMTP_auth=GLOB(0x32a7210)<<< 250-ENHANCEDSTATUSCODES
Net::SMTP_auth=GLOB(0x32a7210)<<< 250-STARTTLS
Net::SMTP_auth= GLOB(0x32a7210)<<< 250-X-ANONYMOUSTLS
Net::SMTP_auth=GLOB(0x32a7210)<<< 250-AUTH NTLM
Net::SMTP_auth=GLOB(0x32a7210)<<< 250-X-EXPS GSSAPI NTLM
Net:: SMTP_auth=GLOB(0x32a7210)<<< 250-8BITMIME
Net::SMTP_auth=GLOB(0x32a7210)<<< 250-BINARYMIME
Net::SMTP_auth=GLOB(0x32a7210)<<< 250-CHUNKING
Net::SMTP_auth=GLOB(0x32a7210 )<<< 250 XRDST
Net::SMTP_auth=GLOB(0x32a7210)>>> AUTH NTLM TlRMTVNTUAAABAAAAB6IAAAMAAwAgAAAAAAAAAAMAAABkd2g=
Net :: SMTP_auth = GLOB (0x32a7210) <<<= 334 TlRMTVNTUAACAAAACAAIADgAAAAFgoECHJxYyefK5M8AAAAAAAAAAI4AjgBAAAAABgOAJQAAAA9CAE8AUABTAAIACABCAE8AUABTAAEAEgBTAFIAVgBFAFgAQwBBAFMAMgAEABQAYgBvAHAAcwAuAGwAbwBjAGEAbAADACgAcwByAHYAZQB4AGMAYQBzADIALgBiAG8AcABzAC4AbABvAGMAYQBsAAUAFABiAG8AcABzAC4AbABvAGMAYQBsAAcACACm7t37ED7RAQAAAAA
Net :: = SMTP_auth GLOB (0x32a7210) >>> TlRMTVNTUAADAAAAGAAYAEAAAAAYABgAWAAAAAgACABwAAAABgAGAHgAAAAGAAYAfgAAAAAAAABEAAAABYKBAnG9ceiZyEyU3wCV8NOAfE8TH + / clRupP7yUlEceMUku8CHdpBvP / hrs861nYznZ / UIATwBQAFMAZAB3AGgAZAB3AGgA
Net :: SMTP_auth = GLOB (0x32a7210) <<< 5.7.3 535 Authentication unsuccessful
<<< Process finished. (Exit code 1)
=============== READY ================

#! /usr/bin/perl -w

sub send_mail {
  use Net::SMTP_auth;
  use Authen::SASL;
  use Authen::NTLM;
  use MIME::Base64;
    
    my ($smtpserver, $to, $from, $msg, $subject, $apass) = @_;
    my $error;
    my $body;
    my $smtp;
    
    $body = "MIME-Version: 1.0\n"
        . "From: $from\n"
        . "To: $to \n"
        . "Subject: $subject\n\n" 
        . $msg . "\n"; 
 
  eval { };
  
    if (!ref($to))
    { 
        @$to = split(/[,;]\s*/, $to); 
    }
    if(!($smtp = Net::SMTP_auth->new($smtpserver, Debug => 1)))
    {
        return "Could not connect to SMTP Server: $smtpserver \n";
    }
  if (length($apass) > 0)
  {		
    if(!($smtp->auth('NTLM', '', '')))
    {
      return $smtp->message(); 
    }
  }
    if (!($smtp->mail($from)))
    {
        return "Bad From, check your Ac count Information " . $smtp->message();
    }
    if (!($smtp->recipient(@$to)))
    {	
        return "Bad EmailTo " . $smtp->message();
    }
    
    $smtp->data($body);
    $smtp->quit;
    
    return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question