Search code examples
stringperlsendmailexists

-e (if file exists) trickiness in perl finding sendmail


Trying to test a list of common sendmail locations, i thought to just loop and -e test, but there is something I'm not understanding here, wrote a little subroutine to explain this better;

sub mail_z
{
my $sendmail = '/xampp/usr/lib/sendmail';  #tried with/without/ and included the .exe

if(-e $sendmail){print"found SendMail";}else{print"NOT Found SendMail";}

    open ( MAIL, "| $sendmail " );
        print MAIL "From: $sender\n";
        print MAIL "To: $recipient\n";
        print MAIL "Subject: $subject\n\n";
        print MAIL "$body\n";
        print MAIL "\n.\n";
    close ( MAIL );
}

The result of this sub is that it prints "NOT Found SendMail" to the page and then uses sendmail to email me. Clearly it finds it just not with -e what am i doing wrong here?


Solution

  • What i wasn't understanding was that in xampp there is a hook to catch the abbreviated [/xampp/usr/lib/sendmail] when using [open MAIL]. Its probably there to simulate an online Apache server but it isn't deep enough to affect -e. The actual path to sendmail on my xampp is [/xampp/usr/lib/sendmail.exe] and this works for both tests. Note; most installations of xampp, sendmail is in [xampp/sendmail/sendmail.exe]

    Doing a readdir test on my local [/xampp/usr/lib/] Dir shows me the file [sendmail.exe] but when i run the same readdir test online host at [/usr/sbin/] i discover the file is called [sendmail] without extension and doing an -f and -d (file or folder) test on [sendmail] online it shows its a file.. So my original problem will be fine once uploaded, i just have to remember to add the exe for local tests.

    Holli and Grinnz answer was a fine alternative route, however i needed to find out what was wrong with my code before proceeding.