Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Simple SMTP email within script...

by jbodden (Initiate)
on Jun 08, 2008 at 02:32 UTC ( #690871=perlquestion: print w/replies, xml ) Need Help??
jbodden has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I humbly seek the wisdom of the Perl Monks on the following challenge that has come my way. I'm new to Perl (from a programming aspect anyway) and new to this site. It definitely seems like the right place to be in order to develop my unused Perl programming skills. Right now I have a specific deliverable that is required and I have determined Perl to be the most effective way of meeting that deadline. I have written a little script that searches a log text file for a specific string, when it finds it I just have it print out that it found it right now. What I need the script to do is to send an email to the helpdesk that simply says "backup failed on server X" in the subject. Thatís all, I thought, it would be simple and straight forward (LOL...) Every thing I can find seems to rely on other scripts and convoluted setups. I will be distributing this to 100's of servers and really would just like to keep everything short and within the same script if possible... Is there a simple way to do this or do I have to find a way to include something like SendMail or Mail:Mailer, etc...

Replies are listed 'Best First'.
Re: Simple SMTP email within script...
by sgifford (Prior) on Jun 08, 2008 at 03:46 UTC
    On Unix the standard way to do this is by running sendmail, mail, or Mail, and if you're on Unix that's what I would do to keep things simple. If you need to run on a non-Unix platform, probably using an existing SMTP module (like Email::Send::SMTP) is your best bet; that will work on any platform.

    It's generally not that hard to install other libraries along with your script. A useful tool for bundling your script and all of the modules it uses into one executable is PAR.

    Update: Fix typo in module name.

Re: Simple SMTP email within script...
by shoness (Friar) on Jun 08, 2008 at 06:12 UTC
    Since you're so close already, your Perl script could simply compose the message in a temp file and then call a Unix utility like "mailx".
Re: Simple SMTP email within script...
by kabeldag (Hermit) on Jun 08, 2008 at 03:50 UTC
    Hi jbodden,

    You should format your posts with <BR>'s :-).

    Another approach would be to create a raw socket to your SMTP server using IO::Socket::INET, which is basically stock in 90% Perl's.Once you have your script working, you can always bundle everything up into an executable using PAR Packager (PP).

Re: Simple SMTP email within script...
by skx (Parson) on Jun 08, 2008 at 10:41 UTC

    If you only care about whether a given string exists in a logfile, and send a mail if so then you might find a shell script to be sufficient?

    #!/bin/sh if ( grep "pattern" /var/log/log.file 2>/dev/null >/dev/null ); then hostname | mail -s "Backup failed" fi

    Of course if you want to learn perl, or if your requirements change, then you could come back to it..

Re: Simple SMTP email within script...
by apl (Monsignor) on Jun 08, 2008 at 13:49 UTC
    If you look at my Re: using the Mailer / sendmail perl module..., you'll see a snippet of Perl code that doesn't rely on modules, which will let you e-mail the status of background tasks. I use this on a regular basis, and am quite happy with it.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://690871]
Approved by planetscape
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2017-02-20 09:50 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (295 votes). Check out past polls.