http://www.perlmonks.org?node_id=1217900


in reply to Re^3: Trouble emailing zip file
in thread Trouble emailing zip file

Hi haukex,

I'm getting the following after running the code below"

Can't exec "/usr/bin/mailx uuencode $zipfile $zipfile -r -s": No such file or directory at ./test-email.pl line 42.

system /usr/bin/mailx uuencode $zipfile $zipfile -r -s logs user@email.com failed: -1 at ./test-email.pl line 42

#!/opt/bin/perl use strict; use warnings; my $home = $ENV{"HOME"}; print "$home\n"; my $zipfile = "$home/file.zip"; print "$zipfile\n"; my $emailcmd = '/usr/bin/mailx uuencode $zipfile $zipfile -r -s'; print "$emailcmd\n"; my $subject = 'logs'; print "$subject\n"; my $recipient = 'user@email.com '; print "$recipient\n"; # takes one argument, the filename to encode # returns one value, the filename of the temporary(!) output file use File::Temp qw/tempfile/; sub uuencode { my ($infile) = @_; my ($tfh,$tfn) = tempfile(UNLINK=>1); open my $fh, '<:raw', $infile or die "$infile: $!"; printf $tfh "begin %03o %s\n", ((stat($infile))[2] & 0666)||0644, $infile; my $buf; print $tfh pack 'u', $buf while read $fh, $buf, 45; print $tfh "`\n", "end\n"; close $fh; close $tfh; return $tfn; } #call uuencode sub with param &uuencode($zipfile); my @args = ("$emailcmd", "$subject", "$recipient"); system(@args) == 0 or die "system @args failed: $?";

Replies are listed 'Best First'.
Re^5: Trouble emailing zip file
by Paladin (Vicar) on Jul 04, 2018 at 23:08 UTC
    When using the LIST form of system() the first argument is the program to run, the rest of the arguments are the parameters for the program. I can almost guarantee you don't have a program called /usr/bin/mailx uuencode $zipfile $zipfile -r -s on your system. You probably have /usr/bin/mailx however. The other parts (uuencode $zipfile $zipfile -r -s) need to be passed as parameters to mailx.
    my @emailcmd = ('/usr/bin/mailx', 'uuencode', $zipfile, $zipfile, '-r' +, '-s'); #and later my @args = (@emailcmd, $subject, $recipient); system(@args) == 0 or die "system @args failed: $?";

    This is of course assuming that the command /usr/bin/mailx uuencode file.zip file.zip -r -s Subject Recipient indeed works on the command line.

Re^5: Trouble emailing zip file
by poj (Abbot) on Jul 04, 2018 at 20:23 UTC

    Use double quotes not single quotes here

    $emailcmd = "/usr/bin/mailx uuencode $zipfile $zipfile -r -s";

    Also the quotes can be removed here

    my @args = ($emailcmd, $subject, $recipient); poj

      still getting the same error and I have confirmed that the zip file does exist.

        still getting the same error ...

        If by "... the same error ..." you mean
            Can't exec "/usr/bin/mailx uuencode $zipfile $zipfile -r -s": No such file or directory at ...
        then you haven't read and/or understood poj's reply.

        Use double-quotes to interpolate the  $zipfile string into the  $emailcmd string:

        c:\@Work\Perl\monks>perl -wMstrict -le "my $home = $ENV{'HOME'}; print qq{>$home<}; ;; my $zipfile = qq{$home/file.zip}; print qq{>>$zipfile<<}; ;; my $emailcmd = qq{/usr/bin/mailx uuencode $zipfile $zipfile -r -s}; print qq{>>>$emailcmd<<<}; " >IsWhereTheHeartIs< >>IsWhereTheHeartIs/file.zip<< >>>/usr/bin/mailx uuencode IsWhereTheHeartIs/file.zip IsWhereTheHeartI +s/file.zip -r -s<<<
        Maybe see discussion of  qq// et al and interpolation in perlop. (Basic problem is with string interpolation and not with existence of a file.)


        Give a man a fish:  <%-{-{-{-<

Re^5: Trouble emailing zip file
by haukex (Archbishop) on Jul 06, 2018 at 11:30 UTC

    Unfortunately, there are several issues with that code. As I documented in the comments, my sub uuencode returns a filename, but you're not using that return value anywhere. Instead, you seem to have just stuck the string "uuencode" into the command right after "mailx" and rearranged the command-line arguments - I'm not sure what you're trying to do there, since I'm pretty sure that doesn't work on the command line either. And Paladin has already identified the issue with your use of system (unfortunately poj's comment was misleading on that issue).

    At this point I have to recommend taking a step back. I know that stuff not working can be frustrating. But on the other hand, guessing at syntax doesn't (always) get you further either. I'd have to suggest taking the time to reread perlintro and the documentation on things like system, as well as my node on that topic, as well as Yes, even you can use CPAN and perhaps A Guide to Installing Modules. Invest some time now into understanding those topics to save yourself time afterwards.