Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sendmail Pipe to PHP: Could not open input file

I've installed sendmail and want to pipe incoming emails to php.

Every time I send an email to my server I am getting an email back with an error message:

could not open input file: /root/fw/catcher.php 554 5.3.0 unknown mailer error 1

I think something with the permissions of catcher.php is wrong but I couldn't figure it out by myself.

Sendmail is installed and I have added an alias:

root: "|/usr/bin/php /root/fw/catcher.php"

Permissions (after chmod 777; I tried chmod 777 and chmod 755 but both won't work):

drwxrwxrwx 2 root root 4096 Jul 20 14:27 fw
-rwxrwxrwx 1 root root   45 Jul 20 14:27 catcher.php

catcher.php (converted line endings to Unix style):

#!/usr/bin/php
<?php echo 'Test'; exit(0); ?>

Executing my php file over cli works fine. All of these commands work:

/usr/bin/php /root/fw/catcher.php
/usr/bin/php7.3 /root/fw/catcher.php
php /root/fw/catcher.php
php7.3 /root/fw/catcher.php

I think there is a problem with the permissions of sendmail.

like image 910
T K Avatar asked Jul 20 '19 14:07

T K


1 Answers

The error "could not open input file" means that the file cannot be read for some reason.

Please follow these steps to check:

  1. converted line endings to Unix style

I do not know how you check this, but you could try to redo this using dos2unix, as described in this link

dos2unix catcher.php > newcatcher.php

and then compare file sizes.

  1. it could be that BOM breaks the script, as described here, the shebang line "#!/usr/bin/php" tells the system which interpreter needs to be run when invoking such a script.

If the script is encoded in UTF-8, one may be tempted to include a BOM at the beginning. But actually the "#!" characters are not just characters. They are in fact a magic number that happens to be composed out of two ASCII characters. If you put something (like a BOM) before those characters, then the file will look like it had a different magic number and that can lead to problems.

The script will run over cli because you specifically tell it which intepreter to use: php or php7.3:

php /root/fw/catcher.php
php7.3 /root/fw/catcher.php

To remove BOM from the start of the file, try this:

dos2unix catcher.php

Try to run /root/fw/catcher.php, as a shell script without using php or php7.3 executable. From terminal, run:

./root/fw/catcher.php
like image 148
Jannes Botis Avatar answered Sep 30 '22 10:09

Jannes Botis