How to Change the MTA return error if something happend when delivery email failed using pipe to php script method

695 views Asked by At

I using WHM/Cpanel

Im using the pipe technique to forward the income emails to php script and everthing fine. but while working if any error happend while piping the following msg for example will returned to the email sender

This message was created automatically by mail delivery software.
  A message that you sent could not be delivered to one or more of its recipients. This is a permanent error. The following address(es) failed:

  pipe to |/home/someuser/public_htmk/pipe.php
  generated by [email protected]
  local delivery failed

Please note that I make the pipe from Cpanel for the email [email protected]|/home/someuser/public_htmk/pipe.php

the php script below does not have error :) but I define the path of the file to make an error because it should be public_html not public_htmk but I make this error to show you the error message which returned to the email sender.

So is there anyway to control of this returned message or disable it . for example change it or not to send the physical address of the php file that we are running to pipe the email atleast?

BTW I'm using

WHM/Cpanel Dovecot PHP

and this is a sample of the pipe script (this script does not have any error)

#!/usr/local/bin/php -q
<?php   

// read from stdin
$emg_stdf = fopen("php://stdin", "r");
$email = "";
while (!feof($emg_stdf))
{
    $emg_orgemailmsg .= fread($emg_stdf, 1024);
}
fclose($emg_stdf);

mail('[email protected]','From my email pipe!','"' . $emg_orgemailmsg . '"');

I'm looking to customize or disable the return msg which returned to the email sender when some error happened when piping email to a script.

any ideas?

3

There are 3 answers

1
Michael Krelin - hacker On BEST ANSWER

If you do not insist on putting troublesome code in the pipe definition, you can use the shell script wrapper around your php script, something like

#!/bin/bash
/home/someuser/public_htmk/pipe.php >&/home/someuser/pipe.errors.log || true

and use it in your pipe definition.

0
drew010 On

This may help your problem in some cases, but if display_errors is turned on, an error message in your script may trigger a negative response from your php script which causes the message to bounce. If you turn it off, then no errors would be output or returned to the MTA.

If you have a fatal error such as a parse error for some reason, then that may not help.

I'm not sure you have any way to control the content of the returned message, but you could possibly prevent it or try to get a message back to it.

It may help to put exit(0); at the end of the email processor to indicate success, so if your script is able to reach the end it exits successfully and may prevent the MTA from sending the return message.

I'm not sure if this makes a difference, but it may help to check if opening php://stdin was successful on the off chance that it could fail for some reason and if you can't read it, then terminate the script.

To try to control the output, if you detect an error, try echo'ing a message or using exit("status message");

In the return messages you get does it ever contain error messages output from PHP or a reason for failure?

1
Anaa On

I had the same problem and I solved it by adding a file named exim.conf in root/etc folder It worked for me Following are the contents of that file Hope it will help others too!

# This transport is used for handling pipe deliveries generated by alias
# or .forward files. If the pipe generates any standard output, it is returned
# to the sender of the message as a delivery error. Set return_fail_output
# instead of return_output if you want this to happen only when the pipe fails
# to complete normally. You can set different transports for aliases and
# forwards if you want to - see the references to address_pipe in the directors
# section below.

address_pipe:
driver = pipe
return_fail_output

virtual_address_pipe:
driver = pipe
group = nobody
return_fail_output
user = "${lookup{$domain}lsearch* {/etc/virtual/domainowners}{$value}}"