send emails via perl

2014-11-19 Thread Sergio Letuche
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


Re: send emails via perl

2014-11-19 Thread Cory Snavely

I've always used this:

http://search.cpan.org/~markov/MailTools-2.13/lib/Mail/Mailer.pod

On 11/19/2014 09:22 AM, Sergio Letuche wrote:

ok this is what i asked, could a mail be sent without mailx been installed,

thank you

2014-11-19 16:19 GMT+02:00 PHILLIPS M.E. m.e.phill...@durham.ac.uk
mailto:m.e.phill...@durham.ac.uk:

Not sure exactly what you mean by not using a mailer.

__ __

In our Perl scripts I tend to do:

__ __

open (MAIL, |-, '/bin/mailx', '-s', $subject, @addresses)

 || die Failed to e-mail report: $!\n;

print MAIL $report;

close MAIL;

__ __

which relies on mailx being installed and able to send messages.

__ __

I’m sure there are SMTP modules for Perl, and things like that which
could do it all at socket level, but I wouldn’t see that as being
easier necessarily. It depends on what control you have over your
server.

__ __

Matthew

__ __

*From:*Sergio Letuche [mailto:code4libus...@gmail.com
mailto:code4libus...@gmail.com]
*Sent:* 19 November 2014 08:31
*To:* perl4lib@perl.org mailto:perl4lib@perl.org
*Subject:* send emails via perl

__ __

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




RE: send emails via perl

2014-11-19 Thread Gorman, Jon

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

Re: send emails via perl

2014-11-19 Thread Marc Chantreux
On Wed, Nov 19, 2014 at 02:19:26PM +, PHILLIPS M.E. wrote:
 open (MAIL, |-, '/bin/mailx', '-s', $subject, @addresses)
 || die Failed to e-mail report: $!\n;  

what's the point of using perl then?

-- 
Marc Chantreux,
Mes coordonnées: http://annuaire.unistra.fr/chercher?n=chantreux
Direction Informatique, Université de Strasbourg (http://unistra.fr) 
Don't believe everything you read on the Internet
-- Abraham Lincoln


Re: send emails via perl

2014-11-19 Thread Sergio Letuche
i do not object to any approach suggested, all are welcome

2014-11-19 16:46 GMT+02:00 Marc Chantreux m...@unistra.fr:

 On Wed, Nov 19, 2014 at 02:19:26PM +, PHILLIPS M.E. wrote:
  open (MAIL, |-, '/bin/mailx', '-s', $subject, @addresses)
  || die Failed to e-mail report: $!\n;

 what's the point of using perl then?

 --
 Marc Chantreux,
 Mes coordonnées: http://annuaire.unistra.fr/chercher?n=chantreux
 Direction Informatique, Université de Strasbourg (http://unistra.fr)
 Don't believe everything you read on the Internet
 -- Abraham Lincoln



RE: send emails via perl

2014-11-19 Thread PHILLIPS M.E.
 On Wed, Nov 19, 2014 at 02:19:26PM +, PHILLIPS M.E. wrote:
  open (MAIL, |-, '/bin/mailx', '-s', $subject, @addresses)
  || die Failed to e-mail report: $!\n;
 
 what's the point of using perl then?

There's more than one way to do it.

If mailx is already installed and configured then this allows you to send your 
e-mail from your Perl script without installing other modules.  I have 
sometimes had to use servers where installing extra modules from CPAN was not 
under my control.

I was assuming that the original poster's script was doing various other things 
and needing to e-mail a report at the end.  Obviously if the script was only 
doing an e-mail then there would not be much point doing it this way

Matthew



Re: send emails via perl

2014-11-19 Thread Marc Chantreux

On Wed, Nov 19, 2014 at 04:47:59PM +0200, Sergio Letuche wrote:
 i do not object to any approach suggested, all are welcome 

based on my own experience (experience feedback is something i expect
when i ask something in a list):

* don't use perl when i don't need it 
  (when it's about sending an email from a cronjob, i use mutt and m4)
* when i have to use perl, want things to be testable and independant
  from the cli tools. There are a lot of good modules to send email,
  MIME::Lite is one of them.

regards

-- 
Marc Chantreux,
Mes coordonnées: http://annuaire.unistra.fr/chercher?n=chantreux
Direction Informatique, Université de Strasbourg (http://unistra.fr) 
Don't believe everything you read on the Internet
-- Abraham Lincoln


Re: send emails via perl

2014-11-19 Thread Marc Chantreux
On Wed, Nov 19, 2014 at 02:51:58PM +, PHILLIPS M.E. wrote:
 If mailx is already installed and configured then this allows you to
 send your e-mail from your Perl script without installing other
 modules.  I have sometimes had to use servers where installing extra
 modules from CPAN was not under my control.

i personnaly use cpanm and local::lib to address those kind of problems.

this is so easy to install a module nowaday we should think twice before
not relying on the CPAN power. 

-- 
Marc Chantreux,
Mes coordonnées: http://annuaire.unistra.fr/chercher?n=chantreux
Direction Informatique, Université de Strasbourg (http://unistra.fr) 
Don't believe everything you read on the Internet
-- Abraham Lincoln