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 john.doe@example.com to jane.doe@example.com with subject "Example Subject" and message body "Hello World".

#!/usr/bin/perl
use Net::SMTP;
use strict;
use warning;

my @recipients = qw(john.doe@example.com jane.doe@example.com);

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

  $smtp->mail("no-reply\@example.com");

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

 

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 smtp.example.com 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)<<< 250-smtp.example.com Hello [10.17.44.52]
Net::SMTP=GLOB(0x3b4c790)<<< 250-SIZE 31457280
Net::SMTP=GLOB(0x3b4c790)<<< 250-PIPELINING
Net::SMTP=GLOB(0x3b4c790)<<< 250-DSN
Net::SMTP=GLOB(0x3b4c790)<<< 250-ENHANCEDSTATUSCODES
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:<john.doe@example.com>
Net::SMTP=GLOB(0x3b4c790)<<< 250 2.1.0 Sender OK
Net::SMTP=GLOB(0x3b4c790)>>> RCPT TO:<jane.doe@example.com>
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: jane.doe@example.com
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 <7fada5c0-90b4-479b-a77d-391f751461d1@smtp.example.com> [InternalId=704374640187, Hostname=smtp.example.com] 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



Comments


Add a Comment


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