below is an inherited script, done sometime in the mid 90s.
it works, but seems very inefficeint - especially all the system calls to do file work. Any hints on doing this better would be appreciated.
_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _
it works, but seems very inefficeint - especially all the system calls to do file work. Any hints on doing this better would be appreciated.
_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _
Code:
#!/usr/bin/perl
print "starting up 1602email \n";
system "ls -1 /mnt/AUTOMAIL/dailywebreport*.* > /tmp/$$";
open EMAIL,"</tmp/$$" or die "unable to open file $!";
my ($ifile, $ofile, $fromaddr, $basename, $aname, $base);
while(<EMAIL>)
{
chomp;
$ifile="$_";
my $size = -s $ifile;
if($size > 1)
{
my $attaches = 0;
if($_ ne "ATTACH")
{
open IFILE,"$ifile" or die "cant open '$ifile' $!";
$ofile="$_.mail";
open OFILE,">$ofile" or die "cant do '$ofile' $!";
$fromaddr='PostMaster';
while(<IFILE>)
{
chomp;
if(substr($_,0,3) eq '@@@')
{
chomp($basename=substr($_,3,(length $_)-4));
$aname="/mnt/automail/ATTACH/" . $basename;
if(not -e "$aname")
{
print "$ifile is missing attachment \n";
}
$base="$aname.base64";
system "/usr/bin/mimencode -o $base <$aname";
print OFILE "\n--Message-Boundary--\n",
"Content-type: Application/octet-stream;name=\"$basename\"\n";
print OFILE "Content-transfer-encoding: BASE64\n\n";
close OFILE;
system "cat $base >> $ofile";
open OFILE,">>$ofile";
system "cp $base /usr/mailAttached/`date +%d`/";
system "rm -f $base";
system "rm -f $aname";
$attaches=1;
};
if(substr($_,0,5) eq "From:")
{
$fromaddr=(substr($_,5));
};
if(substr($_,0,3) ne "\@\@\@")
{
print OFILE $_ ."\n";
};
}
close IFILE;
system "\\rm -f $ifile";
if($attaches==1)
{ print OFILE "\n--Message-Boundary----\n";
$attaches=0;
};
close OFILE;
system "cat $ofile | /usr/bin/sendmail -t -f$fromaddr";
system "\\mv $ofile /mnt/mailgen/";
}
} #check of file size
}
close EMAIL;