In the past I've also done it without using a module by just doing a very bare 
bones Net::SMTP session if I remember right. That's included in perl in most 
recent versions if I remember right.  (I'd have to dig around to find where 
I've used it, it's rare as in most cases I prefer the approach below).

What you might want to do (although more complicated) is install some of the 
log4perl modules and have that send the email when it's done. That’s what we do 
for most of our automated processes. For historical perl scripts, we just 
essentially call in the log4perl modules, configure them to send out an email 
when either  it sees a log message "Finished" or if there's a fatal level.

So I've got an example logger config (see bottom of email), based off the 
log4perl faq. The email client is different, because I couldn't get the email 
module to work on the windows machines that also run some perl scripts here. 
I'd recommend pulling up the faq entry and basing the configuration off of that 
since it's an Ubuntu machine 
(http://log4perl.sourceforge.net/releases/Log-Log4perl/docs/html/Log/Log4perl/FAQ.html#a0f6e)

Then in the perl script I can just add to the top

================= top of perl code ===============

use Log::Log4perl qw(get_logger :levels);

$SIG{__DIE__} = sub {
        if($^S) {
                # We're in an eval {} and don't want log
                # this message but catch it later
                return;
        }
        $Log::Log4perl::caller_depth++;
        my $logger = get_logger('SomeLoggerName');
        $logger->logdie(@_);
};

$logger->info("Process started").
================= top of perl code ===============

And a line to indicate the process actually finished

$logger->info("Process finished");

On some scripts I also capture warnings, but the one I pulled up apparently I 
didn't (indicator there's likely a lot of junk produced by some subprocess)

This particular log config is what I use for some more possibly sensitive 
processes, if there's a fatal error it doesn't include a dump of errors in the 
email, but will do so in the file.

You can modify the email config as well.


======================= logger.config ===========================
log4perl.logger.SomeLoggerName=DEBUG, Log, Screen, Finished, Mailer

log4perl.appender.Log=Log::Dispatch::File
log4perl.appender.Log.filename=billing.log
log4perl.appender.Log.mode=append
log4perl.appender.Log.layout=Log::Log4perl::Layout::PatternLayout
log4perl.appender.Log.layout.ConversionPattern=%d %p>%m %n

log4perl.appender.Screen=Log::Dispatch::Screen
log4perl.appender.Screen.stderr=0
log4perl.appender.Screen.layout=Log::Log4perl::Layout::PatternLayout
log4perl.appender.Screen.layout.ConversionPattern=%d %p> %.50m %n




#
# This is an email alert just letting people know that the feeds
# are done

log4perl.filter.FinishedFilter = sub { /finished/i }


log4perl.appender.Finished           = Log::Dispatch::Email::MailSender
log4perl.appender.Finished.name      = feedsFinished
log4perl.appender.Finished.to        =  jtgor...@illinois.edu, 
somegroupem...@foo.com
log4perl.appender.Finished.from      = feeds-nore...@library.uiuc.edu
log4perl.appender.Finished.subject   = AR load finished
log4perl.appender.Finished.smtp      = mail.library.uiuc.edu
log4perl.appender.Finished.layout=Log::Log4perl::Layout::PatternLayout
log4perl.appender.Finished.layout.ConversionPattern=(%d) %m %n
log4perl.appender.Finished.Threshold = INFO
log4perl.appender.Finished.Filter = FinishedFilter
log4perl.appender.Finished.buffered = 0

#mail::send doesn't seem to work as with the faq, some windows issues...
#doesn't have warning::register but uses if warning::enabled()
#  leads to error, I know there's some way to override, but not sure
#  how to do in this config
#seems like smtp w/ MailSender works after poking around
#various webpages and reading through source files...
# also need to install Mail-Sender (undocumented)

#making the pattern layout just state the time, what file
#but not any actual details, just in case dbi returns a bit too 
#much info.
log4perl.appender.Mailer           = Log::Dispatch::Email::MailSender
log4perl.appender.Mailer.name      = feedsFATAL
log4perl.appender.Mailer.to        = jtgor...@illinois.edu, 
somegroupem...@foo.com
log4perl.appender.Mailer.from      = noreply-fe...@library.uiuc.edu
log4perl.appender.Mailer.subject   = Fatal error with Billing Load
log4perl.appender.Mailer.smtp      = mail.library.uiuc.edu
log4perl.appender.Mailer.layout=Log::Log4perl::Layout::PatternLayout
log4perl.appender.Mailer.layout.ConversionPattern=Error in billing load (%d) 
log4perl.appender.Mailer.Threshold = FATAL
#log4perl.appender.Mailer.buffered  = 0

======================= logger.config ===========================


> -----Original Message-----
> From: baxter.b...@gmail.com [mailto:baxter.b...@gmail.com] On Behalf Of
> Brad Baxter
> Sent: Wednesday, November 19, 2014 8:17 AM
> To: perl4lib
> Subject: Re: send emails via perl
> 
> This might answer that:
> 
> http://perldoc.perl.org/perlfaq9.html#How-do-I-send-email%3f
> 
> 
> On Wed, Nov 19, 2014 at 3:30 AM, Sergio Letuche <code4libus...@gmail.com
> <mailto:code4libus...@gmail.com> > wrote:
> 
> 
>       hello,
> 
>       we need to use the easiest solution, if possible just use a perl
> module, to be able to send automated emails on an Ubuntu server. The
> scenario is this: we ran a cron job, and say we would like to send a message
> after completion, to a certain for example gmail account. The ideal would be
> to not use any mailer, is this possible? Or could you please suggest us the
> best - easiest approach?
> 
>       Thank you
> 

Reply via email to