3

I would like postfix to run a external php script (a hook) every time there is a outgoing delivery error.

For example, my webserver sends out emails and sometimes the recipients mailbox is full or nonexistent:

(host mx2.seznam.cz[77.75.76.32] said: 450 5.1.1 Mailbox is currently disabled. (in reply to RCPT TO command))

I would like to capture those errors through a script and notify my user that he needs to update his email address in our system.

Is it possible to get postfix to dump all of this information (email address, SMTP error) into a external script as they happen?

Thank you.

1 Answers1

1

I ended up rerouting rsyslog maillog notifications via UDP to a simple php daemon for processing and doing all the magic there.

The bounce-daemon.php is controlled by monit, so it's always up and running.

rsyslog.conf:

# Forward postfix logs via UDP 
mail.*                                                  @127.0.0.1:55515

bounce-daemon.php:

$socket = stream_socket_server("udp://127.0.0.1:55515", $errno, $errstr, STREAM_SERVER_BIND);
if (!$socket) {
    die("$errstr ($errno)");
}

do {

    //  echo "Waiting for data... ";
    $pkt = stream_socket_recvfrom($socket, 10000, 0, $peer);

    // do processing here
    echo "$pkt\n";

} while ($pkt !== false);