It turns out that cPanel allows you to pipe emails to a script under Email Forwards. I just had to enter the relative path to my PHP script and have the appropriate hash-bang on the first line of my script, which in this case was
#!/usr/bin/php -q
Through a combination of this thread and some further research here is the script that works for me:
#!/usr/bin/php -q
<?php
// start output buffering
ob_start();
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
// handle email
$lines = explode("\n", $email);
// empty vars
$from = "";
$subject = "";
$headers = "";
$message = "";
$splittingheaders = true;
for($i=0; $i<count($lines); $i++) {
if($splittingheaders) {
// this is a header
$headers .= $lines[$i]."\n";
// look out for special headers
if(preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
if(preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
} else {
// not a header, but message
$message .= $lines[$i]."\n";
}
if(trim($lines[$i])=="") {
// empty line, header section has ended
$splittingheaders = false;
}
}
// clean the output
ob_end_clean();
?>