Bootstrap FreeKB - Perl (Scripting) - Net::SMTP (email)
Perl (Scripting) - Net::SMTP (email)

Updated:   |  Perl (Scripting) articles

The Net::SMTP module can be installed using cpan or cpanm.

cpanm Net::SMTP


Here is the minimum markup needed to send an email. In this example, the email will be sent from to with subject "Example Subject" and message body "Hello World".

use Net::SMTP;
use strict;
use warning;

my @recipients = qw(;

foreach my $recipient (@recipients) {
  my $smtp = Net::SMTP->new(
    Host => "",
    Debug => 1


  if ($smtp->to($recipient)) {
    $smtp->datasend("To: $recipient \n");
    $smtp->datasend("Subject: Example Subject \n");
    $smtp->datasend("Hello World");
  else {
   print "Error: ", $smtp->message();


When Debug => 0 is used, no output should be produced. Debug => 1 can be used to output debugging evets, something like this.

Net::SMTP>>> Net::SMTP(2.31)
Net::SMTP>>>   Net::Cmd(2.29)
Net::SMTP>>>     Exporter(5.68)
Net::SMTP>>>   IO::Socket::INET(1.33)
Net::SMTP>>>     IO::Socket(1.34)
Net::SMTP>>>       IO::Handle(1.33)
Net::SMTP=GLOB(0x3b4c790)<<< 220 Microsoft ESMTP MAIL Service ready at Wed, 21 Jul 2021 06:17:56 -0500
Net::SMTP=GLOB(0x3b4c790)>>> EHLO localhost.localdomain
Net::SMTP=GLOB(0x3b4c790)<<< Hello []
Net::SMTP=GLOB(0x3b4c790)<<< 250-SIZE 31457280
Net::SMTP=GLOB(0x3b4c790)<<< 250-PIPELINING
Net::SMTP=GLOB(0x3b4c790)<<< 250-DSN
Net::SMTP=GLOB(0x3b4c790)<<< 250-STARTTLS
Net::SMTP=GLOB(0x3b4c790)<<< 250-8BITMIME
Net::SMTP=GLOB(0x3b4c790)<<< 250-BINARYMIME
Net::SMTP=GLOB(0x3b4c790)<<< 250-CHUNKING
Net::SMTP=GLOB(0x3b4c790)<<< 250 SMTPUTF8
Net::SMTP=GLOB(0x3b4c790)>>> MAIL FROM:<>
Net::SMTP=GLOB(0x3b4c790)<<< 250 2.1.0 Sender OK
Net::SMTP=GLOB(0x3b4c790)>>> RCPT TO:<>
Net::SMTP=GLOB(0x3b4c790)<<< 250 2.1.5 Recipient OK
Net::SMTP=GLOB(0x3b4c790)>>> DATA
Net::SMTP=GLOB(0x3b4c790)<<< 354 Start mail input; end with <CRLF>.<CRLF>
Net::SMTP=GLOB(0x3b4c790)>>> To:
Net::SMTP=GLOB(0x3b4c790)>>> Subject: Example Subject 
Net::SMTP=GLOB(0x3b4c790)>>> Hello World
Net::SMTP=GLOB(0x3b4c790)>>> .
Net::SMTP=GLOB(0x3b4c790)<<< 250 2.6.0 <> [InternalId=704374640187,] 1703 bytes in 0.256, 6.490 KB/sec Queued mail for delivery
Net::SMTP=GLOB(0x3b4c790)>>> QUIT
Net::SMTP=GLOB(0x3b4c790)<<< 221 2.0.0 Service closing transmission channel


Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee


Add a Comment

Please enter 9bb1d4 in the box below so that we can be sure you are a human.