Perl code, I don't understand the error outputted - perl

#!perl
use warnings;
use strict;
use Net::SMTP;
my $smtpserver = 'server';
my $smtpport = 25;
my $smtpuser = 'username';
my $smtppassword = 'password';
my $smtp = Net::SMTP->new($smtpserver, Port=>$smtpport, Timeout => 10, Debug => 1);
die "Could not connect to server!\n" unless $smtp;
$smtp->auth($smtpuser, $smtppassword);
$smtp->mail('sender#example.com');
$smtp->to('mymail#gmail.com');
$smtp->data();
$smtp->datasend("To: mymail\#gmail.com\n");
$smtp->datasend("bla bla bla ... \n");
$smtp->quit;
When I execute this code I got the below output. I don't have much more knowledge about perl but the project is Perl, thus I must do it this way.
What is happening below: I do not understand it personally.
Net::SMTP>>> Net::SMTP(2.29)
Net::SMTP>>> Net::Cmd(2.26)
Net::SMTP>>> Exporter(5.58)enter code here
Net::SMTP>>> IO::Socket::INET(1.27)
Net::SMTP>>> IO::Socket(1.28)
Net::SMTP>>> IO::Handle(1.24)
Net::SMTP=GLOB(0x7a6280)<<< 220 server ESMTP Sendmail 8.13.1/8.13.1; Sun, 6 Dec 2015 19:34:41 +0530
Net::SMTP=GLOB(0x7a6280)>>> EHLO localhost.localdomain
Net::SMTP=GLOB(0x7a6280)<<< 250-server Hello localhost.localdomain [127.0.0.1], pleased to meet you
Net::SMTP=GLOB(0x7a6280)<<< 250-ENHANCEDSTATUSCODES
Net::SMTP=GLOB(0x7a6280)<<< 250-PIPELINING
Net::SMTP=GLOB(0x7a6280)<<< 250-8BITMIME
Net::SMTP=GLOB(0x7a6280)<<< 250-SIZE
Net::SMTP=GLOB(0x7a6280)<<< 250-DSN
Net::SMTP=GLOB(0x7a6280)<<< 250-ETRN
Net::SMTP=GLOB(0x7a6280)<<< 250-AUTH GSSAPI DIGEST-MD5 CRAM-MD5
Net::SMTP=GLOB(0x7a6280)<<< 250-DELIVERBY
Net::SMTP=GLOB(0x7a6280)<<< 250 HELP
Net::SMTP=GLOB(0x7a6280)>>> MAIL FROM:<sendermail>
Net::SMTP=GLOB(0x7a6280)<<< 250 2.1.0 <sendermail>... Sender ok
Net::SMTP=GLOB(0x7a6280)>>> RCPT TO:<receipient mail>
Net::SMTP=GLOB(0x7a6280)<<< 250 2.1.5 <recipient mail>... Recipient ok
Net::SMTP=GLOB(0x7a6280)>>> DATA
Net::SMTP=GLOB(0x7a6280)<<< 354 Enter mail, end with "." on a line by itself
Net::SMTP=GLOB(0x7a6280)>>> To: mymail#gmail.com
Net::SMTP=GLOB(0x7a6280)>>> .
Net::SMTP=GLOB(0x7a6280)<<< 250 2.0.0 tB6EJbum025820 Message accepted for delivery
Net::SMTP=GLOB(0x7a6280)>>> QUIT
Net::SMTP=GLOB(0x7a6280)<<< 221 2.0.0 server closing connection
I will be very grateful for any help — thanks in advance.

I don't understand the error outputted
You didn't show us any error. As you can see, the server said "Message accepted for delivery".
The missing ->dataend might mean you are sending an empty email, though.
What is happening below
An email message is being sent. As well, messages to help you debug problems are being outputted since you requested these using Debug => 1.

Related

Perl Net::SMTP abruptly disconnects when/before calling auth()

What I have is a relatively simple Perl script that will be given a string of json data, parse out specific properties and pretty-print the rest, wrap it all up in an email and send it out via my mail server. It's been two decades since I wrote any Perl but I'm fairly confident my code is alright.
Unfortunately, the script fails when it goes to authenticate to the postfix daemon via submission. It doesn't actually fail to authenticate. From the postfix logs it doesn't appear to even attempt to authenticate. After the server responds to the second EHLO (EHLO->STARTTLS->EHLO) the script appears to just abruptly disconnect. Is there some trick to making Net::SMTP authenticate that I'm missing here?
The resulting output is:
Net::SMTP>>> Net::SMTP(3.11)
Net::SMTP>>> Net::Cmd(3.11)
Net::SMTP>>> Exporter(5.73)
Net::SMTP>>> IO::Socket::IP(0.39)
Net::SMTP>>> IO::Socket(1.40)
Net::SMTP>>> IO::Handle(1.40)
Net::SMTP=GLOB(0x55d6336b7108)<<< 220 mx.example.net ESMTP Postfix (Ubuntu)
Net::SMTP=GLOB(0x55d6336b7108)>>> EHLO tiphares.example.net
Net::SMTP=GLOB(0x55d6336b7108)<<< 250-mx.example.net
Net::SMTP=GLOB(0x55d6336b7108)<<< 250-PIPELINING
Net::SMTP=GLOB(0x55d6336b7108)<<< 250-SIZE 10240000
Net::SMTP=GLOB(0x55d6336b7108)<<< 250-ETRN
Net::SMTP=GLOB(0x55d6336b7108)<<< 250-STARTTLS
Net::SMTP=GLOB(0x55d6336b7108)<<< 250-ENHANCEDSTATUSCODES
Net::SMTP=GLOB(0x55d6336b7108)<<< 250-8BITMIME
Net::SMTP=GLOB(0x55d6336b7108)<<< 250-DSN
Net::SMTP=GLOB(0x55d6336b7108)<<< 250 SMTPUTF8
Net::SMTP=GLOB(0x55d6336b7108)>>> STARTTLS
Net::SMTP=GLOB(0x55d6336b7108)<<< 220 2.0.0 Ready to start TLS
Net::SMTP::_SSL=GLOB(0x55d6336b7108)>>> EHLO tiphares.example.net
Net::SMTP::_SSL=GLOB(0x55d6336b7108)<<< 250-mx.example.net
Net::SMTP::_SSL=GLOB(0x55d6336b7108)<<< 250-PIPELINING
Net::SMTP::_SSL=GLOB(0x55d6336b7108)<<< 250-SIZE 10240000
Net::SMTP::_SSL=GLOB(0x55d6336b7108)<<< 250-ETRN
Net::SMTP::_SSL=GLOB(0x55d6336b7108)<<< 250-AUTH PLAIN LOGIN
Net::SMTP::_SSL=GLOB(0x55d6336b7108)<<< 250-ENHANCEDSTATUSCODES
Net::SMTP::_SSL=GLOB(0x55d6336b7108)<<< 250-8BITMIME
Net::SMTP::_SSL=GLOB(0x55d6336b7108)<<< 250-DSN
Net::SMTP::_SSL=GLOB(0x55d6336b7108)<<< 250 SMTPUTF8
Can't Authenticate:
mx.example.net
PIPELINING
SIZE 10240000
ETRN
AUTH PLAIN LOGIN
ENHANCEDSTATUSCODES
8BITMIME
DSN
SMTPUTF8
SASL(0): successful result: at /usr/local/sbin/suricata-alert-mail.pl line 134.
It's worth noting that other mail clients (ie. Thunderbird) have no issue with sending mail using the same server and port.
UPDATE: I've come to the conclusion there is nothing wrong with my code. So, as Steffen Ullrich alluded to in the comments, I suspect this is an issue with my environment.
These are my installed modules according to cpan -l.

Sendmail use localhost.localdomain

Sendmail with Perl uses localhost.localdomain instead of a fully qualified domain name (FQDN). Sendmail and server configuration is correct, hostname is setup with FQDN.
My script contains the following lines:
use MIME::Lite;
use Email::Sender::Simple qw /sendmail/;
use Email::Sender::Transport::SMTPS;
...
my $transport = Email::Sender::Transport::SMTPS->new({
host => $server,
ssl => 'starttls',
port => $port,
sasl_username => $user,
sasl_password => $password,
debug => 1,
});
sendmail($msg->as_string, { transport => $transport });
Mail is sent successfully, however sendmail use localhost.localdomain instead of FQDN with EHLO. Debug information shows:
Net::SMTPS>>> Net::SMTPS(0.09)
Net::SMTPS>>> IO::Socket::IP(0.39)
Net::SMTPS>>> IO::Socket(1.40)
Net::SMTPS>>> IO::Handle(1.40)
Net::SMTPS>>> Exporter(5.73)
Net::SMTPS>>> Net::SMTP(3.11)
Net::SMTPS>>> Net::Cmd(3.11)
Net::SMTPS=GLOB(0x2903e48)<<< 220 smtp.gmail.com ESMTP l186sm18879092ioa.54 - gsmtp
Net::SMTPS=GLOB(0x2903e48)>>> EHLO localhost.localdomain
Net::SMTPS=GLOB(0x2903e48)<<< 250-smtp.gmail.com at your service, [masked IP address]
Net::SMTPS=GLOB(0x2903e48)<<< 250-SIZE 35882577
Net::SMTPS=GLOB(0x2903e48)<<< 250-8BITMIME
Net::SMTPS=GLOB(0x2903e48)<<< 250-STARTTLS
Net::SMTPS=GLOB(0x2903e48)<<< 250-ENHANCEDSTATUSCODES
Net::SMTPS=GLOB(0x2903e48)<<< 250-PIPELINING
Net::SMTPS=GLOB(0x2903e48)<<< 250-CHUNKING
Net::SMTPS=GLOB(0x2903e48)<<< 250 SMTPUTF8
Net::SMTPS=GLOB(0x2903e48)>>> STARTTLS
Net::SMTPS=GLOB(0x2903e48)<<< 220 2.0.0 Ready to start TLS
Net::SMTPS=GLOB(0x2903e48)>>> EHLO localhost.localdomain
Net::SMTPS=GLOB(0x2903e48)<<< 250-smtp.gmail.com at your service, [masked IP address]
Net::SMTPS=GLOB(0x2903e48)<<< 250-SIZE 35882577
Net::SMTPS=GLOB(0x2903e48)<<< 250-8BITMIME
Net::SMTPS=GLOB(0x2903e48)<<< 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
Net::SMTPS=GLOB(0x2903e48)<<< 250-ENHANCEDSTATUSCODES
Net::SMTPS=GLOB(0x2903e48)<<< 250-PIPELINING
Net::SMTPS=GLOB(0x2903e48)<<< 250-CHUNKING
Net::SMTPS=GLOB(0x2903e48)<<< 250 SMTPUTF8
When sendmail is used from the console directly (not using Perl), correct FQDN is used with EHLO.
Net::SMTPS=GLOB(0x2903e48)>>> EHLO localhost.localdomain
From the documentation of Email::Sender::Transport::SMTPS:
ATTRIBUTES
The following attributes may be passed to the constructor:
...
helo: what to say when saying HELO; no default
If nothing is given in helo the default from the underlying packet (Net::SMTPS) is used, which is localhost.localdomain. To use something different just do
my $transport = Email::Sender::Transport::SMTPS->new({
...,
helo => 'whatever.you.want.to.use.instead.of.localhost.localdomain'
});

Emacs (24.4.2) Gnus (0.12) TLS connection not working

Recently, I've been experiencing problems with my email setup using Emacs (24.4.2) + Gnus (0.12). It seems that the TSL connection is not working:
gnutls.c: [0] (Emacs) fatal error: The TLS connection was non-properly terminated.
gnutls.el: (err=[-110] The TLS connection was non-properly terminated.) boot: (:priority NORMAL :hostname smtp.me.com :loglevel 0 :min-prime-bits 256 :trustfiles nil :crlfiles nil :keylist nil :verify-flags nil :verify-error nil :callbacks nil)
smtpmail-send-it: Sending failed: 530 5.7.0 No STARTTLS command has been given.
Is it a Gnus or Openssl problem? How should I debug and fix it?
Here is the output of the smtp trace buffer:
220 nk11p00mm-asmtp003.example.com -- Server ESMTP (Oracle Communications Messaging Server 7u4-27.10(7.0.4.27.9) 64bit (built Jun 6 2014))
250-nk11p00mm-asmtp003.example.com
250-8BITMIME
250-PIPELINING
250-CHUNKING
250-DSN
250-ENHANCEDSTATUSCODES
250-EXPN
250-HELP
250-XADR
250-XSTA
250-XCIR
250-XGEN
250-XLOOP 098E7F59418D8F04E0C7135651497A4E
250-STARTTLS
250-NO-SOLICITING
250 SIZE 28311552
220 2.5.0 Go ahead with TLS negotiation.
Process smtpmail deleted
220 nk11p00mm-asmtp003.example.com -- Server ESMTP (Oracle Communications Messaging Server 7u4-27.10(7.0.4.27.9) 64bit (built Jun 6 2014))
MAIL FROM:<john.doe#example.com>
250-nk11p00mm-asmtp003.example.com
250-8BITMIME
250-PIPELINING
250-CHUNKING
250-DSN
250-ENHANCEDSTATUSCODES
250-EXPN
250-HELP
250-XADR
250-XSTA
250-XCIR
250-XGEN
250-XLOOP 098E7F59418D8F04E0C7135651497A4E
250-STARTTLS
250-NO-SOLICITING
250 SIZE 28311552
RCPT TO:<john.doe#example.com>
530 5.7.0 No STARTTLS command has been given.
QUIT
503 5.5.0 No MAIL FROM command has been issued.

SMTP 450 when sending mail with Net::SMTP

SO.
I'm failing at sending mail from my Perl script. This is what my code looks like:
use Net::SMTP;
$smtp = Net::SMTP->new("smtpserver.mydomain.com");
$smtp->mail("myemail\#mydomain.com");
$smtp->to("myemail\#mydomain.com");
$smtp->data();
$smtp->datasend("From: myemail\#mydomain.com\n");
$smtp->datasend("To: myemail\#mydomain.com\n");
$smtp->datasend("Subject: Test message\n");
$smtp->datasend("Just a test message.\n");
$smtp->dataend();
$smtp->quit;
This is what the debug option throws:
Net::SMTP>>> Net::SMTP(2.31)
Net::SMTP>>> Net::Cmd(2.29)
Net::SMTP>>> Exporter(5.64_03)
Net::SMTP>>> IO::Socket::INET(1.31)
Net::SMTP>>> IO::Socket(1.32)
Net::SMTP>>> IO::Handle(1.31)
Net::SMTP=GLOB(0x1d1a06c)<<< 220 ESMTP Postfix
Net::SMTP=GLOB(0x1d1a06c)>>> EHLO localhost.localdomain
Net::SMTP=GLOB(0x1d1a06c)<<< 250-
Net::SMTP=GLOB(0x1d1a06c)<<< 250-PIPELINING
Net::SMTP=GLOB(0x1d1a06c)<<< 250-SIZE
Net::SMTP=GLOB(0x1d1a06c)<<< 250-ETRN
Net::SMTP=GLOB(0x1d1a06c)<<< 250-STARTTLS
Net::SMTP=GLOB(0x1d1a06c)<<< 250-ENHANCEDSTATUSCODES
Net::SMTP=GLOB(0x1d1a06c)<<< 250-8BITMIME
Net::SMTP=GLOB(0x1d1a06c)<<< 250 DSN
Net::SMTP=GLOB(0x1d1a06c)>>> MAIL FROM:<myemail#mydomain.com>
Net::SMTP=GLOB(0x1d1a06c)<<< 250 2.1.0 Ok
Net::SMTP=GLOB(0x1d1a06c)>>> RCPT TO:<myemail#mydomain.com>
Net::SMTP=GLOB(0x1d1a06c)<<< 450 4.7.1 <localhost.localdomain>: Helo command rejected: Service temporarily unavailable
Net::SMTP=GLOB(0x1d1a06c)>>> DATA
Net::SMTP=GLOB(0x1d1a06c)<<< 554 5.5.1 Error: no valid recipients
Net::SMTP=GLOB(0x1d1a06c)>>> From: myemail#mydomain.com
Net::SMTP=GLOB(0x1d1a06c)>>> To: myemail#mydomain.com
Net::SMTP=GLOB(0x1d1a06c)>>> Subject: Test message
Net::SMTP=GLOB(0x1d1a06c)>>> Just a test message.
Net::SMTP=GLOB(0x1d1a06c)>>> .
Net::SMTP=GLOB(0x1d1a06c)<<< 221 2.7.0 Error: I can break rules, too. Goodbye.
Net::SMTP=GLOB(0x1d1a06c)>>> QUIT
The interesting part is that the SMTP server replies with 250 OK when I talk directly to it and I receive the mail just fine:
220 ESMTP Postfix
MAIL FROM:<myemail#mydomain.com>
250 2.1.0 Ok
RCPT TO:<myemail#mydomain.com>
250 2.1.5 Ok
DATA
354 End data with <CR><LF>.<CR><LF>
Just a test message.
.
250 2.0.0 Ok: queued as B68672C02B
Does someone have some light to shed?
Thanks in advance.
Just posting this as an answer instead so that it can be accepted as the answer, as requested by triplee.
Something might be wrong in your domain setup.
also add $smtp->datasend("\n"); underneath $smtp->datasend("Subject: Test message\n"); in a new line, else it will not show mesage text.
like this.
$smtp->datasend("Subject: Test message\n");
$smtp->datasend("\n");
$smtp->datasend("Just a test message.\n");
The SMTP server (postfix) "does not like" hostname used in EHLO command but rejects it in reply to RCPT TO: command.
>>> MAIL FROM:<myemail#mydomain.com>
<<< 250 2.1.0 Ok
....
>>> RCPT TO:<myemail#mydomain.com>
<<< 450 4.7.1 <localhost.localdomain>: Helo command rejected: Service temporarily unavailable
You can make Net::SMTP use another hostname in HELO/EHLO command by passing Hello parameter to new.
$smtp = Net::SMTP->new('smtpserver.mydomain.com', Hello => 'host.example.net');

Why does Perl's MIME::Lite fail with " SMTP data() command failed: 2.1.5 ... Recipient ok"?

This is probably a crossover question that is valid for both SO and SF.
I am using a Perl script as follows to send e-mail:
my $SMTP_SERVER = 'xx.xx.xx.xx';
my $DEFAULT_SENDER = 'yyy#xxx.com';
my $DEFAULT_RECIPIENT = 'yyy#xxx.com';
### Create the multipart "container":
$msg = MIME::Lite->new(
From =>$DEFAULT_SENDER,
To =>$input{to},
Cc =>$DEFAULT_SENDER,
Subject =>$input{mailsubject},
Type =>'multipart/mixed'
);
my $msgbody="<body bgcolor=\"#C0C0C0\">\n";
$msgbody.="<img src="cid:xyz.gif"><br>O hai thar.."
$msgbody.="</body>";
### Add the text message part:
### (Note that "attach" has same arguments as "new"):
$msg->attach(
Type =>'text/html',
Data =>$msgbody
);
### Add the logo image part:
$msg->attach(
Type => 'image/gif',
Id => 'xyz.gif',
Path => '/var/www/images/xyz.gif',
);
### Add the file part:
$msg->attach(
Type =>'application/zip',
Path =>$input{fullpath},
Filename =>$input{file},
Disposition => 'attachment'
);
open ERROR, '>>', "/debug/error.txt" or die $!;
STDERR->fdopen( \*ERROR, 'w' );
eval{
# send the email
MIME::Lite->send('smtp', $SMTP_SERVER, Timeout=>30, Debug=>1, Notify => ['FAILURE','DELAY'], SkipBad => 1 );
$msg->send();
1;
}
or do{&error($#)};
close ERROR;
Now this script works lovely for 98% of the e-mails sent.. however.. the big 2% client is the one I am trying to figure out now. Whenever I send to this client in particular I get the message: SMTP data() command failed: 2.1.5 ... Recipient ok .
Has anyone seen this error before or have any idea as to where my problem is? I tried replacing the X-Mailer: header thinking that was being spam filtered. I tried sending the same message to others on our domain and that works just not to this client URL.
example header below:
MIME-Version: 1.0
Content-Transfer-Encoding: binary
Content-Type: multipart/mixed;
boundary="_----------=_12845827796770"
X-Mailer: MIME::Lite 3.027 (F2.76; T1.29; A2.03; B3.07_01; Q3.07)
Date: Wed, 15 Sep 2010 13:37:59 -0700
From: yyy#xxx.com
To: client1#clienturl.com, client2#clienturl.com, me#xxx.com
Cc: yyy#xxx.com
Subject: TEST
Any clues?
Also, sending an e-mail directly using Outlook with same attachments works perfectly with this client and same smtp server. It is only when being sent via this automated process that the error rears it's ugly head.
Update:
Debug Log (which has me thinking it is a big latency issue where the responses are coming back asynch and hosing up the normal flow of operations in the process of MIME::Lite)
MIME::Lite::SMTP>>> MIME::Lite::SMTP
MIME::Lite::SMTP>>> Net::SMTP(2.31)
MIME::Lite::SMTP>>> Net::Cmd(2.29)
MIME::Lite::SMTP>>> Exporter(5.62)
MIME::Lite::SMTP>>> IO::Socket::INET(1.31)
MIME::Lite::SMTP>>> IO::Socket(1.30_01)
MIME::Lite::SMTP>>> IO::Handle(1.27)
MIME::Lite::SMTP=GLOB(0x84ac258)<<< 220 smpt.xxx.com ESMTP Sendmail 8.12.9/8.13.1; Wed, 15 Sep 2010 16:50:44 -0700 (PDT)
MIME::Lite::SMTP=GLOB(0x84ac258)>>> EHLO localhost.localdomain
MIME::Lite::SMTP=GLOB(0x84ac258)<<< 250-smpt.xxx.com Hello [xx.xx.xx.xx], pleased to meet you
MIME::Lite::SMTP=GLOB(0x84ac258)<<< 250-ENHANCEDSTATUSCODES
MIME::Lite::SMTP=GLOB(0x84ac258)<<< 250-PIPELINING
MIME::Lite::SMTP=GLOB(0x84ac258)<<< 250-8BITMIME
MIME::Lite::SMTP=GLOB(0x84ac258)<<< 250-SIZE
MIME::Lite::SMTP=GLOB(0x84ac258)<<< 250-DSN
MIME::Lite::SMTP=GLOB(0x84ac258)<<< 250-ETRN
MIME::Lite::SMTP=GLOB(0x84ac258)<<< 250-DELIVERBY
MIME::Lite::SMTP=GLOB(0x84ac258)<<< 250 HELP
MIME::Lite::SMTP=GLOB(0x84ac258)>>> MAIL FROM:<yyy#xxx.com>
MIME::Lite::SMTP=GLOB(0x84ac258)<<< 250 2.1.0 <yyy#xxx.com>... Sender ok
MIME::Lite::SMTP=GLOB(0x84ac258)>>> RCPT TO:<client1#clienturl.com>
[Wed Sep 15 16:41:22 2010] myemailscript.cgi: MIME::Lite::SMTP=GLOB(0x84ac258): Timeout at /usr/local/share/perl/5.10.0/MIME/Lite.pm line 2889
MIME::Lite::SMTP=GLOB(0x84ac258)>>> RCPT TO:<client2#clienturl.com>
[Wed Sep 15 16:41:52 2010] myemailscript.cgi: MIME::Lite::SMTP=GLOB(0x84ac258): Timeout at /usr/local/share/perl/5.10.0/MIME/Lite.pm line 2889
MIME::Lite::SMTP=GLOB(0x84ac258)>>> RCPT TO:<me#xxx.com>
[Wed Sep 15 16:42:22 2010] myemailscript.cgi: MIME::Lite::SMTP=GLOB(0x84ac258): Timeout at /usr/local/share/perl/5.10.0/MIME/Lite.pm line 2889
MIME::Lite::SMTP=GLOB(0x84ac258)>>> RCPT TO:<yyy#xxx.com>
MIME::Lite::SMTP=GLOB(0x84ac258)<<< 250 2.1.5 <client1#clienturl.com>... Recipient ok
MIME::Lite::SMTP=GLOB(0x84ac258)>>> DATA
MIME::Lite::SMTP=GLOB(0x84ac258)<<< 250 2.1.5 <client2#clienturl.com>... Recipient ok
The Net::SMTP client appears to be timing out, but the responses to the RCTP TO's are arriving eventually when they are no longer expected. Try setting the timeout to 300 seconds instead of 30?