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

Hi,

I am having a problem using the mailx mailer with system. The email body is not being sent in the email. The mailx command works fine from the command line, reading in the logfile as the message body. Unfortunatly using a mail PM like Mail::Mailer is not an option. Any suggesstions?

my $status=system("mailx -s \"$subject\" $address < /opt/autotree/auto +user/out/chk_cal_date.out") warn "Mailer exited with error: $?" unless $status == 0

Replies are listed 'Best First'.
Re: Using system with mailx
by Abigail-II (Bishop) on Aug 06, 2003 at 13:36 UTC
    Did you get an exit value other than 0? Did you get any warnings from mailx? What's the content of your mailer log (/var/log/mail.log or something like that)?

    A message of "it's not being sent" doesn't give us much clues on what could be wrong. Specially not because the most likely cause is a non-Perl one.

    Abigail

Re: Using system with mailx
by zby (Vicar) on Aug 06, 2003 at 13:37 UTC
    I can't say if mailx do that, but some programs check if they are executed by human (or rather if the input is a tty). You might check the output of the mailx program by running it in backtics instead of system.
Re: Using system with mailx
by blue_cowdawg (Monsignor) on Aug 06, 2003 at 14:19 UTC

        The email body is not being sent in the email.
    As others in this thread have stated there are many reasons why this could be happening and without amplifying information there is no way to tell what is going on.

    Nevertheless let me forge on with a suggested change in your strategy:

        #!/usr/bin/perl -w use strict; use diagnostics; use warnings; open(FIN,"< /opt/autotree/autouser/out/chk_cal_date.out") or die $!; my $old_ifs=$/; # Lets nullify the IRS. $/=undef; my $slurp=<FIN>; $/=$old_ifs; # Restore the IFS close FIN; send_the_mail('you@there.com','a subject',$slurp); exit(0); sub send_the_mail { my($addr,$subject,$body)=@_; open(PIPE,sprintf("| mailx -s %s %s",$subject,$addr)) or die $!; print <PIPE> $body; close PIPE; } __END__

        Unfortunatly using a mail PM like Mail::Mailer is not an option.
    I am mildly curious: why isn't this an option? It would certainly be a much cleaner approach in my honest opinion...

    Hope this helps.


    Peter @ Berghold . Net

    Sieze the cow! Bite the day!

    Test the code? We don't need to test no stinkin' code! All code posted here is as is where is unless otherwise stated.

    Brewer of Belgian style Ales

      I agree, but the mail PM are not installed on all of the servers and I can't get the SA's to install them.

      Thanks for the suggestions. I will check the log files as pointed out in the other posts and see if I can figure out the problem.

        What makes you think you need the SA to install a module?
        $ perldoc -q 'own module'

        Abigail

      In addition to checking the result of the open() call, I would also recommend checking the result of the close() call, especially when the file handle is opened to a piped program.
          :
          close PIPE 
              or die $!;
          :
      
        I just read "perldoc -f close" which leads me to an even better example showing a non-zero exit status. If mailx is having problems, not checking the result of the close() call could easily hide those problems.
            close PIPE
                or die $! || "exit status $?";
        

        While you are technically correct I have actually seen cases where turning warnings on will produce spew falsely about using "or die" on close. I just tried to duplicate it but I don't remember where I ran into it. (AIX?)


        Peter @ Berghold . Net

        Sieze the cow! Bite the day!

        Test the code? We don't need to test no stinkin' code! All code posted here is as is where is unless otherwise stated.

        Brewer of Belgian style Ales

Re: Using system with mailx
by Limbic~Region (Chancellor) on Aug 06, 2003 at 15:00 UTC
    cutter,
    Try this:
    #!/usr/bin/perl -w use strict; my $subject = 'test message'; my $address = 'email@address.com'; my $file = '/opt/autotree/autouser/out/chk_cal_date.out; my $status = system("cat $file | mailx -s \"$subject\" $address"); #my $status = system("uuencode $file $file | mailx -s \"$subject\" $ad +dress"); warn "Mailer exited with error: $?" unless $status == 0;
    I added the uuencode in case you wanted it as an attachment and not just body text. I am pretty sure that at least one mailer module (Net::SMTP) comes standard with the Perl CORE though?

    Cheers - L~R