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

hi,

I can't get mailx to send from within a script. All other UNIX commands work fine when suurounded by single quotes. any ideas?

e.g. '/bin/mailx -s "hello" john@work.com'

or

system("/bin/mailx -s "hello" john@work.com");

thanks, Ad

  • Comment on can't get mailx to send from within a script

Replies are listed 'Best First'.
Re: can't get mailx to send from within a script
by gellyfish (Monsignor) on Aug 17, 2006 at 12:10 UTC

    Doesn't mailx need some input on it's STDIN, like the message for instance? I think you need to do something like (untested) :

    open MAILX,'|/bin/mailx -s "hello" john@work.com' or die $!; print MAILX 'Your message here'; close MAILX or die "mailx: $! $?";

    I would however recommend using another mechanism to send mail in your program, not limited to piping directly to sendmail or using a module such as Email::Send

    /J\

      Gellyfish,

      not sure why the other suggestions didn't work, but yours did so that's what I went with.

      As it happens I needed to email the contents of a file and found this to work :

      $file="/tmp/output.txt";

      my $text = do { local( @ARGV, $/ ) = $file ; <> } ;

      open MAILX,'|/bin/mailx -s "Output" John@work.com' or die $!;

      print MAILX $text;

      close MAILX or die "mailx: $! $?";

      many thanks to all who contributed, Ad

Re: can't get mailx to send from within a script
by davido (Cardinal) on Aug 17, 2006 at 12:10 UTC

    Hopefully this plain-text illustration is clear enough to highlight the problem with your second examaple.

    system("/bin/mailx -s "hello" john@work.com"); ^ ^ ^ ^ ^ | | | | | quote | | quote unquote | bareword unquote

    It ought to be a "bareword found where operator expected" error.


    Dave

Re: can't get mailx to send from within a script
by nimdokk (Vicar) on Aug 17, 2006 at 12:09 UTC
    The system command won't work with all double quotes like you have. The initial double quote around hello closes the double quote you started before /bin/mailx. Try using single quotes around hello. You might also try using qx!! or even one of the mail modules (i.e. Net::SMTP) to send your mail.
Re: can't get mailx to send from within a script
by LittleGreyCat (Scribe) on Aug 17, 2006 at 12:48 UTC
    As suggested elsewhere 'mailx' expects some data to send.

    An example; `echo "test message" | /bin/mailx -s "hello" john@work.com` should probably work.

    Check out the command under a shell prompt and make sure you copy it EXACTLY into the "backticks"; if you have to hit additional return keys or anything similar then it won't work as you first tried it.

    HTH

    Dave R

    Nothing succeeds like a budgie with no teeth.