Route the incoming messages to a PHP script using procmail or similar. Your PHP script parses the headers, does what it wants, and sends a response. Which part in particular are you having trouble with?