Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

send emails via perl

19 views
Skip to first unread message

Sergio Letuche

unread,
Nov 19, 2014, 3:45:02 AM11/19/14
to perl...@perl.org
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

Brad Baxter

unread,
Nov 19, 2014, 9:30:02 AM11/19/14
to perl4lib

Cory Snavely

unread,
Nov 19, 2014, 9:30:02 AM11/19/14
to perl...@perl.org
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.ph...@durham.ac.uk
> <mailto:m.e.ph...@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:code4l...@gmail.com
> <mailto:code4l...@gmail.com>]
> *Sent:* 19 November 2014 08:31
> *To:* perl...@perl.org <mailto:perl...@perl.org>
> *Subject:* send emails via perl____
>
> __ __
>
> hello,____
>
> __ __
> you please suggest us the best - easiest approach?____
>
> __ __
>
> Thank you____
>
>

PHILLIPS M.E.

unread,
Nov 19, 2014, 9:30:02 AM11/19/14
to perl...@perl.org

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

Sergio Letuche

unread,
Nov 19, 2014, 9:30:02 AM11/19/14
to PHILLIPS M.E., perl...@perl.org
ok this is what i asked, could a mail be sent without mailx been installed,

thank you

Sergio Letuche

unread,
Nov 19, 2014, 9:45:02 AM11/19/14
to Cory Snavely, perl...@perl.org
thank you all for your answers,

for sure there is more than one way, have to further investigate all links provided here

2014-11-19 16:24 GMT+02:00 Cory Snavely <csna...@umich.edu>:
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.ph...@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:code4libuserx@gmail.com
    <mailto:code4libuserx@gmail.com>]

Gorman, Jon

unread,
Nov 19, 2014, 9:45:02 AM11/19/14
to Brad Baxter, perl4lib

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 = jtgo...@illinois.edu, somegro...@foo.com
log4perl.appender.Finished.from = feeds-...@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 = jtgo...@illinois.edu, somegro...@foo.com
log4perl.appender.Mailer.from = norepl...@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 ===========================

Sergio Letuche

unread,
Nov 19, 2014, 10:00:02 AM11/19/14
to Marc Chantreux, PHILLIPS M.E., perl...@perl.org
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 +0000, 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

Marc Chantreux

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

PHILLIPS M.E.

unread,
Nov 19, 2014, 10:00:02 AM11/19/14
to Marc Chantreux, perl...@perl.org
> On Wed, Nov 19, 2014 at 02:19:26PM +0000, 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

Marc Chantreux

unread,
Nov 19, 2014, 10:15:02 AM11/19/14
to PHILLIPS M.E., perl...@perl.org
On Wed, Nov 19, 2014 at 02:51:58PM +0000, 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

unread,
Nov 19, 2014, 10:15:02 AM11/19/14
to Sergio Letuche, PHILLIPS M.E., perl...@perl.org

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
0 new messages