Search code examples
perlemailsmtpamazon-sessmtp-auth

Error: need MAIL command when using smtp with perl


I was trying to send emails using Mime::Lite perl module and through smtp authentication. But unfortunately that doesn't work. It shows

Error: need MAIL command, Error: command not implemented

Here is the updated code snippet and Debug output.

#!/usr/bin/perl
use warnings;
use strict;
use MIME::Lite;
use Net::SMTP;
use MIME::Base64;


my $smtp = Net::SMTP->new(<mail host>,Port=>587,Debug=>1)or die;
$smtp->starttls();
$smtp->auth($username,$password) or die $!;
my $msg = MIME::Lite -> new ( 
  From  => '[email protected]',
  TO    => '[email protected]', 
  Subject  => 'Testing Text Message',
  Data     => 'How\'s it going.' );

$smtp->mail(<from mail>);
$smtp->to(<to mail>);
$smtp -> data();
$smtp -> datasend( $msg->as_string() );
$smtp -> dataend(); 
print $smtp ->message();
$smtp -> quit;

Debug output:

Net::SMTP>>> Net::SMTP(3.10)
Net::SMTP>>>   Net::Cmd(3.10)
Net::SMTP>>>     Exporter(5.68)
Net::SMTP>>>   IO::Socket::INET6(2.71)
Net::SMTP>>>     IO::Socket(1.36)
Net::SMTP>>>       IO::Handle(1.34)
Net::SMTP=GLOB(0x1e0a920)<<< 220 email-smtp.amazonaws.com ESMTP SimpleEmailService-2108164273 5RjAQr5ZFI284sDt1KWu
Net::SMTP=GLOB(0x1e0a920)>>> EHLO localhost.localdomain
Net::SMTP=GLOB(0x1e0a920)<<< 250-email-smtp.amazonaws.com
Net::SMTP=GLOB(0x1e0a920)<<< 250-8BITMIME
Net::SMTP=GLOB(0x1e0a920)<<< 250-SIZE 10485760
Net::SMTP=GLOB(0x1e0a920)<<< 250-STARTTLS
Net::SMTP=GLOB(0x1e0a920)<<< 250-AUTH PLAIN LOGIN
Net::SMTP=GLOB(0x1e0a920)<<< 250 Ok
Net::SMTP=GLOB(0x1e0a920)>>> STARTTLS
Net::SMTP=GLOB(0x1e0a920)<<< 220 Ready to start TLS
Net::SMTP::_SSL=GLOB(0x1e0a920)>>> EHLO localhost.localdomain
Net::SMTP::_SSL=GLOB(0x1e0a920)<<< 250-email-smtp.amazonaws.com
Net::SMTP::_SSL=GLOB(0x1e0a920)<<< 250-8BITMIME
Net::SMTP::_SSL=GLOB(0x1e0a920)<<< 250-SIZE 10485760
Net::SMTP::_SSL=GLOB(0x1e0a920)<<< 250-STARTTLS
Net::SMTP::_SSL=GLOB(0x1e0a920)<<< 250-AUTH PLAIN LOGIN
Net::SMTP::_SSL=GLOB(0x1e0a920)<<< 250 Ok
Died at test_script.pl line 17.

Please let me know the solution for this.

Thank you in advance!


Solution

  • Your code does not have any error checking and that's why you've missed that the authentication failed:

    Net::SMTP_auth=GLOB(0x13085a8)>>> AUTH LOGIN
    Net::SMTP_auth=GLOB(0x13085a8)<<< 530 Must issue a STARTTLS command first
    

    And because the authentication failed it will not accept sending a mail, i.e. $smtp->mail('[email protected]'); will result in Error: need MAIL command, Error: command not implemented.

    Unfortunately, the very very old Net::SMTP_auth (last update 2006) does not have support for STARTTLS. But, current versions of Net::SMTP have support for both auth (so you don't need Net::SMTP_auth) and starttls (starting with Net::SMTP version 3.xx).

    With Net::SMTP 3.xx your code should look something like this:

    my $smtp = Net::SMTP->new( '<emailhost>') or die;
    $smtp->starttls or die;
    $smtp->auth('<username>', '<password>') or die;
    ...