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

Hi All, I am trying to use mutt command to mail using perl. My code is as follows. There are 3 files viper1.csv, viper2.csv and viper3.csv in the directory /main/attachments. I am trying to attach send mail with one file attached at a time to abc@gmail.com with subject as mailtemplate.txt.
$fileName = 'viper'; $main::mailTemplate = '/main/etc/mailtemplate.txt' $main::outputDir = '/main/attachments'; $main::delAddress = 'abc@gmail.com'; chdir $main::outputDir or die "can't cd"; @files = `ls|grep $fileName`; foreach(@files) { $main::delFileName = $_; print $main::delFileName; #prints the correct file name my $result = `cat $main::mailTemplate|mutt -s "TITLE OF MAIL" -a $main +::outputDir\/$main::delFileName $main::delAddress`; }
The error i am getting is "Error - sh: line 1: abc@gmail.com: command not found"
When I replicate the code for just one file it works.
$main::mailTemplate = '/main/etc/mailtemplate.txt' $main::outputDir = '/main/attachments'; $main::delAddress = 'abc@gmail.com' $main::delFileName = 'viper1.csv'; my $result = `cat $main::mailTemplate|mutt -s "MAIL TITLE" -a $main::o +utputDir\/$main::delFileName $main::delAddress`;

Replies are listed 'Best First'.
Re: Mailing Using Perl
by NetWallah (Canon) on Jun 12, 2009 at 05:38 UTC
    Try the following
    ... foreach(@files) { chomp; ## Add this line.. $main::delFileName = $_;

         Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)

Re: Mailing Using Perl
by cdarke (Prior) on Jun 12, 2009 at 07:15 UTC
    Off topic. Sorry, but I can't help but suggest that you might like to reconsider:
    @files = `ls|grep $fileName`;
    which would be inefficient even in a shell script. Maybe consider globbing (filename expansion/wildcards) instead?
    @files = glob("*$fileName*");
    This is assuming that $fileName does not contain an RE.
    Update: Corrected glob construct