Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Net::SMTP and SSL timeout problem

by vitoco (Friar)
on Jun 09, 2023 at 13:27 UTC ( #11152709=perlquestion: print w/replies, xml ) Need Help??

vitoco has asked for the wisdom of the Perl Monks concerning the following question:


I'm using the standard procedure of the Net::SMTP module to send emails from a Windows box with Strawberry, and it works if I use a plain SMTP connection, but when I use SSL it hangs at the dataend method (which is part of the Net::Cmd module).

I searched and found some other reports, but not a solution.

The piece of code is:

my $smtp = Net::SMTP->new(Host => $mailserver, SSL => 0, Debug => 1, + Timeout => 30); $smtp->auth($username, $password); $smtp->mail($myemail); if ($smtp->to($destemail)) { $smtp->data(); $smtp->datasend("From: $myemail\n"); $smtp->datasend("To: $destemail\n"); $smtp->datasend("Date: $date\n"); $smtp->datasend("Subject: A test\n"); $smtp->datasend("\n"); warn "(end of header)\n"; $smtp->datasend("This is a test...\n"); warn "(end of body)\n"; $smtp->dataend; warn "(dataend completed)\n"; } else { print "ERROR: ", $smtp->message(); } warn "(bye...)\n"; $smtp->quit;

With SSL=>1, debug trace shows all the messages from the (successful) authentication up to "end of body" message and hangs for a while, then a timeout message appears.

I also tried without the dataend, but the quit seems to call dataend internally and the problem persists.

What am I missing?

I don't want to hack standard mods...

Replies are listed 'Best First'.
Re: Net::SMTP and SSL timeout problem
by Bod (Vicar) on Jun 09, 2023 at 20:21 UTC

      Yes, I'm using the latest version. Might it be that I'm missing an SSL setting for that module?

Re: Net::SMTP and SSL timeout problem
by hippo (Bishop) on Jun 09, 2023 at 21:56 UTC

    I use Net::SMTPS and it works very well for the task. I have no idea how well/poorly it performs on MSWin32.


      I only changed my script to use Net::SMTPS module and the email was sent because it ignored the original SSL option in the new method and used a standard connection. Then I added the doSSL option and the script failed with a certificate error:

      DEBUG: .../IO/Socket/ local error: hostname verification failed

      It's strange, because using Net::SMTP there was no certificate error as it authenticated through SSL, accepted the mail header and body, but it did not finished the connection (sending the "." after the body).

      BTW, in the meantime, I tried to use Email::Sender::Simple which I used in another project some years ago, and now it also failed with a timeout. I checked, and it uses Net::SMTP methods!!!

Re: Net::SMTP and SSL timeout problem
by vitoco (Friar) on Jun 10, 2023 at 03:47 UTC

    SOLVED... (I think!)

    I lied with the sample code in my first post: the real code had some international chars (utf8) in the Subject field, which I translated into English for you.

    For an unknown reason, those chars were not a problem for a standard connection to the SMTP server, but somehow locked the connection if it was using SSL, and a timeout from the server was the result.

    Also, I hacked to send "\r\n.\r\n" to the SMTP server at the dataend method instead of just ".\r\n" as an email body terminator while in SSL connection and the result was an email without timeout to be sent, but the header was corrupted, and that gave me a hint. After I encoded the Subject line, all was working like a charm.

    I'll continue testing and let you know what is going on... I still have to add attachments to the email, so more encoding is on the go.

      My guess (due to experience with my own network code) is that the module you are using is directly doing send() on the socket object in this case. send() (almost) always expects character codes below 256, otherwise you'll get a "wide character" error. Depending on the exact implementations, those characters may be dropped or an error might be raised.

      Sidenote: Proper Unicode handling depends heavily on the age of the module, the experience of the author and also partially if the author is a native english speaker living in a country that is using mostly english to communicate. Basically, if the author is using Umlauts or Emoji or Chinese/Japanese/Korean script on a daily basis, the chances Unicode is handled properly by their code is much higher.

      An easy way to really stress-test modules for their Unicode handling is Acme::Umlautify.

      PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP

      I'm pretty sure all the MIME header fields must be encoded as 7-bit ascii even if you use 8-bit unicode of some sort in the body content-type. A MIME-generating module can take care of that for you. I recommend Email::MIME which lets you use header_str to specify unicode strings for headers, and then it automatically encodes them for you.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11152709]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2023-09-22 14:26 GMT
Find Nodes?
    Voting Booth?

    No recent polls found