'postfix pipe to php script: how to fully log php errors/warning/notices?
In postfix on Debian 10 I’m piping received mail to a php script – in master.cf:
mail-pipe-script unix - n n - - pipe flags=DRhu user=mail-pipe-user argv=/usr/bin/php /var/www/html/admin/mail_pipe.php
all works well but any php errors or warnings are returned to postfix and logged in truncated form to /var/log/mail.log - I’d like to view the whole error it its own log file, or at least in mail.log without truncation. The closest I’ve got is at the top of mail_pipe.php:
#!/usr/bin/php
<?php
$x= exec('command 2>> /var/log/mail.log');
But permissions are denied and of course fatal errors won’t get logged. I‘m a bit jumpy about lowering log permissions. I’ve also tried other log destinations. mail_pipe.php is owned as mail-pipe-user:www-data BTW mail_pipe.php reads the email using stdin:
$fd = fopen("php://stdin", "r");
Can someone advise a better way forward for fully logging the php scripts errors and warnings etc?
Thanks!
Solution 1:[1]
AFAIK PHP standard I/O streams cannot be redefined, only closed.
If you have access to script source code, you could define an error handler via set_error_handler.
If not there's an option to wrap your script with another, that will take control over targets I/O streams.
<?php
// Call me wrapper.php
$pipes = [];
$real_process = proc_open(
'php ./wrapped.php',
[
["pipe", "r"],
["pipe", "w"],
["pipe", "w"]
],
$pipes
);
if( is_resource($real_process) ){
// Passing my stdin as-is
fwrite($pipes[0], stream_get_contents(STDIN));
fclose($pipes[0]);
$real_process_said = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$real_process_whined = stream_get_contents($pipes[2]);
fclose($pipes[2]);
proc_close($real_process);
print "STDOUT was: $real_process_said";
print "STDERR was: $real_process_whined";
}
<?php
// Call me wrapped.php
print "The STDIN is: " . stream_get_contents(STDIN);
fwrite(STDERR, "i'm wrapped pleas halp!");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jared |
