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

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.

Related

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'
});

Perl code, I don't understand the error outputted

#!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.

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 server returns no auth methods

I am trying to set up my university smtp server with wanderlust/emacs, and on getting a repeated error decided to have a look via telnet.
the only trouble is, knowing next to nothing about smtp, I can't understand the output:
telnet pod51016.outlook.com 587
Trying 157.56.253.22...
Connected to pod51031.outlook.com.
Escape character is '^]'.
220 DBXPR06CA012.outlook.office365.com Microsoft ESMTP MAIL Service ready at Tue, 30 Sep 2014 16:44:11 +0000
ehlo test
250-DBXPR06CA012.outlook.office365.com Hello [129.234.0.21]
250-SIZE 78643200
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-8BITMIME
250-BINARYMIME
250 CHUNKING
I had expected to see an auth line as 250-AUTH LOGIN PLAIN --- II am just missing something? How do I see what authentication the server supports?
Alternatively, I have it working with thunderbird, so if anyone knows how to extract it from prefs.js that would do.
many thanks, and apologies for the lack of specificity in the question,
John
The server requires you to use establish a secure connection: Use STARTTLS, and the auth methods will appear.
You can test this with openssl:
openssl s_client -starttls smtp -connect server:587

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');