Sending emails from Perl via SMTP server¶
Email inbox could be a good place to store many informations including logs from your services. Common usage of email may be sending notification about your service failures. Email systems are based on a store-and-forward model. Email servers accept, forward, deliver and store messages. So users don’t need be online simultaneously for exchange messages, online presence is only required for time that takes to send or receive messages. So when you send some message it will be delivered to recipient sooner or later.
Dependicies (Debian/Ubuntu)¶
Script below depends on Mail::Sender
module, which may be installed using apt-get install libmail-sender-perl
.
Sending emails from Perl¶
#!/usr/bin/perl
use strict;
use warnings;
use Mail::Sender;
my $smtp = 'smtp.poczta.onet.pl';
my $smtp_port = 587;
my $from = 'src@poczta.onet.pl';
my $pass = 'password';
my $dmail = 'dest@mail.com';
my $subj = 'Hello';
my $msg = 'MSG!';
my $sender = new Mail::Sender {
smtp => $smtp,
from => $from,
auth => 'LOGIN',
port => $smtp_port,
authid => $from,
authpwd => $pass,
on_errors => 'undef',
# debug_level => 4,
# debug => \*STDERR,
} ;
$sender->MailMsg({to => $dmail,subject => $subj,msg => $msg })
or die "Unable to send mail: ".$Mail::Sender::Error ."\n";