简体   繁体   中英

Unable to submit email on port 465 using Net::SMTP module in Perl

I want to submit email on port 465 of my smtp server using the Net::SMTP module(without using Net::SMTP::SSL) in a perl script from client. On Port 465 of my SMTP server, "submissions" service runs, which understands SMTPS.

I have tried to find the way to do this on google. Then used the Net::SMTP::SSL module to make request on port 465. It works fine.

But the documentation for Net::SMTP::SSL recommends using latest version Net::SMTP instead of using Net::SMTP::SSL. The document clearly states that

Since Net::SMTP v1.28 (2014-10-08), Net::SMTP itself has support for SMTP over SSL, and also for STARTTLS. Use Net::SMTP, not Net::SMTP::SSL.

I have updated Net::SMTP module to the latest version 3.11.

Also the document for Net::SMTP also stated clearly that

With IO::Socket::SSL installed it also provides support for implicit and explicit TLS encryption, ie SMTPS or SMTP+STARTTLS.

The part of my perl script code on client ,relevant to the problem mentioned looks like so:

$smtp = Net::SMTP::SSL->new("$mailserver",
                        Hello => "$localhostname",
                        Timeout => 60,
                        Port => "$port",  //Port value is 465
                        Debug => 1);
 $smtp->auth($username, $password);

...Remaining script which sets sender, receiver body etc

This works fine. The email gets submitted. Replacing the above code with :

$smtp = Net::SMTP->new("$mailserver",
                        Hello => "$localhostname",
                        Timeout => 60,
                        Port => "$port",  //Port value is 465
                        Debug => 1);
$smtp->auth($username, $password);

...Remaining script which sets sender, receiver body etc

This fails. The debug logs look like this:

Net::SMTP>>> Net::SMTP(3.11)
Net::SMTP>>>   Net::Cmd(3.11)
Net::SMTP>>>     Exporter(5.73)
Net::SMTP>>>   IO::Socket::INET(1.39)
Net::SMTP>>>     IO::Socket(1.39)
Net::SMTP>>>       IO::Handle(1.39)
Net::SMTP: Net::Cmd::getline(): unexpected EOF on command channel:  at fescommon/mailsend-new.pl line 67.
Can't call method "auth" on an undefined value at fescommon/mailsend-new.pl line 74.

Note: Modules like Net::SMTP, Net::SMTP::SSL, IO::Socket::SSL and others are all updated to the latest version.

Expected Result is that request on "submissions" service listening on port 465 on SMTP server can be made using latest Net::SMTP module, without using Net::SMTP::SSL (because the document claims)

If you want to use smtps (ie TLS from start instead TLS after the STARTTLS command) you have to explicitly say so. Net::SMTP does not magically derive this requirement from the port number. From the documentation :

new ( [ HOST ] [, OPTIONS ] )
SSL - If the connection should be done from start with SSL, contrary to later upgrade with starttls. You can use SSL arguments as documented in IO::Socket::SSL, but it will usually use the right arguments already.

Thus, the proper code should be:

$smtp = Net::SMTP->new($mailserver,
    SSL => 1,  # <<<<<<<<<<<<<<<<<<<<<<<< THIS IS IMPORTANT
    Hello => $localhostname,
    Timeout => 60,
    Port => $port,  # Port value is 465
    Debug => 1
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM