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

I am seeking to execute a simple system command within a perl program:

mail myname@whatever.com -s "Comments -- Version: $Version ID:  $ID" < blah6.txt

basically, I want to be able to send mail based on the contents of a text file and with a few key variables ($Version, $ID) in the subject line to myself. This command

system('mail','myname@whatever.com','-s','Comments -- Version: $Version ID:  $ID','<','blah6.txt');

does not yield optimal results. It sends an email correctly, but the email is blank instead of filled with blah6.txt, and the two variables in the subject line are not recognized. Any ideas what is going on with this? What am I doing wrong?

Also, if there is an easier way of accomplishing what I described, I'd be glad to hear it.

--blanket

Replies are listed 'Best First'.
Re: System Command Issues
by tlm (Prior) on Jun 08, 2005 at 20:42 UTC

    The '<' is something that the shell interprets, but in the multi-arg form of system the shell is bypassed altogether, and '<' is being passed as another argument to mail. Try this instead:

    my $address = 'myname@whatever.com'; my $subject = "Comments -- Version: $Version ID: $ID"; system("mail $address -s '$subject' < blah6.txt") == 0 or die "mail failed: $?";

    Update: As ikegami's reply below alludes to, a major reason for using the multi-arg form of system is precisely to bypass the shell, and therefore avoid shell quoting headaches. The single argument form of system may not be suitable if $Version or $ID contain problematic characters. If this is the case, I would break down and read in the contents of the input file, and pipe it into the mail command from within the Perl code.

    the lowliest monk

      You better make sure $Version and $ID don't contain single quotes or slashes or ... if you use this version of system.
      Followed your advice to the letter. I get a message sent page as I should, but the message never arrives. Any thoughts?

        That sounds to me like a bad e-mail address. If the e-mail address was given in a double-quoted string, did you escape (i.e. backslash) the @? (If not, in addition to doing that, you should be running this under strict, because under strict an unescaped @ in a double-quoted string would have most likely caused the compilation to fail, thereby alerting you to the problem; it's a good thing to do in general.) And, as ikegami noted, what are the strings in $Version and $ID?

        BTW, I tried a similar short script, and it worked fine; this is what I ran:

        use strict; use warnings; my $address = 'myaddress@myhost.com'; my $subject = 'nothing'; system( "mail $address -s $subject < random.txt" ) == 0 or die "mail failed: $?";

        the lowliest monk

Re: System Command Issues
by sk (Curate) on Jun 08, 2005 at 20:45 UTC
Re: System Command Issues
by eyepopslikeamosquito (Archbishop) on Jun 08, 2005 at 21:29 UTC

    To send mail from a Perl program, you have at least four choices:

    1. Use a MUA (mail user agent). e.g. mail, mailx
    2. Use a MTA (mail transport agent), e.g. sendmail
    3. Connect to a SMTP server
    4. Use a CPAN module

    I personally use the Mail::Mailer CPAN module, which hides these choices behind a simple interface. The recent Perl Email Project, see Email::Simple, started by Simon Cozens and now maintained by Casey West, is worth a look, though I haven't used it.

      Mail::Mailer is OK but forks a process without telling you, which can cause some goofiness that bit me in the ass.

      See http://perlmonks.org/?node_id=459739

      I recommend MIME::Simple.