jamgill has asked for the wisdom of the Perl Monks concerning the following question:

Hello. This is my first post.

I have been working on a program that should notify me of certain things via email. I seek comments on the solution described in the code below.

#!/usr/bin/perl -w # $Id: mailtest.pl,v 1.1 2001/08/10 19:32:21 jamgill Exp $ # this little ditty demonstrates using /bin/mail from # within a perl program. it sends emaill to the address # with a subject line of the program's name and a message # of "hello world" and the date. use strict; my $binmail = "/bin/mail"; my $address = 'my_address@my.corneroftha.net'; my $message; my $subject = $0; ### Main: $message = &generate_message($message); `$binmail -t $address <<STOP Subject: $subject hello world. $message STOP`; print "sent message with subject $0 to $address\n"; ### Subs: sub generate_message { my $timestamp = `date`; return($timestamp); }
There are some restrictions that have brought me to /bin/mail: first, This will run on unix boxes with very little access to any modules that are not already installed. second, sendmail will probably not be available.

And the &generate_message, well ... that's just a placeholder for now.

Any comments on sending mail? Is this the best way to do it? Is this a decent way to do it?

Replies are listed 'Best First'.
Re: mail out of /bin/mail
by tadman (Prior) on Aug 11, 2001 at 03:51 UTC
    If you're making no assumptions other than having Perl installed, as some systems are shipped in a condition that can only be described as being both spartan and antiquated, then you've got the right idea.

    If you're willing to allow for modules, you could use something like Mail::Sender.

    It is quite trivial to write an SMTP application like that module, using IO::Socket to help you out. However, you will not get the same reliability as you would with /bin/mail, since you will have to handle your own retries.
Re: mail out of /bin/mail
by clintp (Curate) on Aug 11, 2001 at 00:44 UTC
    If your restrictions are valid, then yeah this is okay if you have absolute control over the body, subject and address of the message. Otherwise it's sometimes possible to sent /bin/mail commands to be processed as part of the mail message body or trick the shell into running a command on your behalf.

    So it's valid, just not recommended. Instead think about using one of the mail modules (Net::SMTP comes to mind) instead.