Sendmail Pipe to PHP: 无法打开输入文件
Sendmail Pipe to PHP: Could not open input file
我已经安装了 sendmail 并希望通过管道将收到的电子邮件发送到 php。
每次我向我的服务器发送电子邮件时,我都会收到一封返回错误消息的电子邮件:
could not open input file: /root/fw/catcher.php 554 5.3.0 unknown
mailer error 1
我认为 catcher.php
的权限是错误的,但我自己无法弄清楚。
Sendmail 已安装,我已添加别名:
root: "|/usr/bin/php /root/fw/catcher.php"
权限(在 chmod 777
之后;我尝试了 chmod 777
和 chmod 755
但都不起作用):
drwxrwxrwx 2 root root 4096 Jul 20 14:27 fw
-rwxrwxrwx 1 root root 45 Jul 20 14:27 catcher.php
catcher.php(将行结尾转换为 Unix 样式):
#!/usr/bin/php
<?php echo 'Test'; exit(0); ?>
通过 cli 执行我的 php 文件工作正常。所有这些命令都有效:
/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
我认为sendmail的权限有问题
错误“无法打开输入文件”表示由于某种原因无法读取文件。
请按照以下步骤检查:
converted line endings to Unix style
我不知道你是如何检查的,但你可以尝试使用
重做
dos2unix catcher.php > newcatcher.php
然后比较文件大小。
- 可能是 BOM 破坏了脚本,如 here 所述,shebang 行 "#!/usr/bin/php" =47=] 告诉系统在调用这样的脚本时需要哪个解释器 运行。
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.
脚本将 运行 over cli 因为你明确告诉它使用哪个解释器:php或 php7.3:
php /root/fw/catcher.php
php7.3 /root/fw/catcher.php
要从文件开头删除 BOM,请尝试 this:
dos2unix catcher.php
尝试 运行 /root/fw/catcher.php,作为 shell 脚本而不使用 php 或php7.3 可执行文件。从终端,运行:
./root/fw/catcher.php
您检查过SELinux是否启用并执行了吗?您可以使用 getenforce
命令查看。
如果 returns Enforcing
,您可以通过几种方式解决此问题。您可以使用 setenforce Permissive
禁用 SELinux 强制执行,或者创建允许 Apache 运行 发送邮件的策略。
要创建这样的策略,最简单的方法是使用 audit2allow
工具,它是 policycoreutils-python
的一部分 - 如果 audit2allow
不可用,请安装该工具.然后,检查/var/log/audit.log
,看看是否有关于sendmail 不能运行 的错误。这些错误行可以通过管道传输到 audit2allow
以创建可以使用 semodule -i <module.pp>
.
启用的策略文件
我已经安装了 sendmail 并希望通过管道将收到的电子邮件发送到 php。
每次我向我的服务器发送电子邮件时,我都会收到一封返回错误消息的电子邮件:
could not open input file: /root/fw/catcher.php 554 5.3.0 unknown mailer error 1
我认为 catcher.php
的权限是错误的,但我自己无法弄清楚。
Sendmail 已安装,我已添加别名:
root: "|/usr/bin/php /root/fw/catcher.php"
权限(在 chmod 777
之后;我尝试了 chmod 777
和 chmod 755
但都不起作用):
drwxrwxrwx 2 root root 4096 Jul 20 14:27 fw
-rwxrwxrwx 1 root root 45 Jul 20 14:27 catcher.php
catcher.php(将行结尾转换为 Unix 样式):
#!/usr/bin/php
<?php echo 'Test'; exit(0); ?>
通过 cli 执行我的 php 文件工作正常。所有这些命令都有效:
/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
我认为sendmail的权限有问题
错误“无法打开输入文件”表示由于某种原因无法读取文件。
请按照以下步骤检查:
converted line endings to Unix style
我不知道你是如何检查的,但你可以尝试使用
dos2unix catcher.php > newcatcher.php
然后比较文件大小。
- 可能是 BOM 破坏了脚本,如 here 所述,shebang 行 "#!/usr/bin/php" =47=] 告诉系统在调用这样的脚本时需要哪个解释器 运行。
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.
脚本将 运行 over cli 因为你明确告诉它使用哪个解释器:php或 php7.3:
php /root/fw/catcher.php
php7.3 /root/fw/catcher.php
要从文件开头删除 BOM,请尝试 this:
dos2unix catcher.php
尝试 运行 /root/fw/catcher.php,作为 shell 脚本而不使用 php 或php7.3 可执行文件。从终端,运行:
./root/fw/catcher.php
您检查过SELinux是否启用并执行了吗?您可以使用 getenforce
命令查看。
如果 returns Enforcing
,您可以通过几种方式解决此问题。您可以使用 setenforce Permissive
禁用 SELinux 强制执行,或者创建允许 Apache 运行 发送邮件的策略。
要创建这样的策略,最简单的方法是使用 audit2allow
工具,它是 policycoreutils-python
的一部分 - 如果 audit2allow
不可用,请安装该工具.然后,检查/var/log/audit.log
,看看是否有关于sendmail 不能运行 的错误。这些错误行可以通过管道传输到 audit2allow
以创建可以使用 semodule -i <module.pp>
.