Email piping in cpanel with php
Ever wanted to automate email processing, or setup a dynamic email response system?
It’s easier than you think. In this example, we will make a simple reply email script, that adds a friendly message and sends your email back to you.
First, you will need to setup an email forwarder within cpanel to point to your script. (or where your script will be)
Second, create your php file. I have provided a working example below that you can use and build off of, but lets go over a few of the parts you may need to modify for your particular environments.
#!/usr/bin/php -q
Since this script wont be processed by the web server, you need to tell the server to run it as a php file. Check with your webhost to find the proper location for php in your particular envionment.
<?php
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
This is where we actually obtain the email message
$lines = explode("\n", $email);
$from = "";
$subject = "";
$headers = "";
$message = "";
$splittingheaders = true;
for ($i=0; $i < count($lines); $i++) {
if ($splittingheaders) {
$headers .= $lines[$i]."\n";
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
} else {
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
$splittingheaders = false;
}
}
In this snippet, we break apart the email by “\n” line breaks and establish our variables. Then we roll through the parts looking for the headers, the subject, the from address, and the message. Once this is said and done, you have all the pieces of your incoming message stored in their respective variables, ready to be handled. This is where you can do some magic and play with the message however you like, such as storing it in a database, or performing on function on the data. Now lets get ready to send a reply message back.
$to = "$from";
$subject = "This is a piped email.";
$body = "$from just sent an email with the subject "$subject", and the message "$message"";
This seems confusing, but all you are doing is sending the email “to” the person that you received the email “from”. You can change the subject to whatever you would like to send back, in this case we added some text to the front of the original subject. The $body we setup includes our own text, as well as the contents of the original subject and message that was sent. Now that we have our own message to send, we can proceed with the next step.
$headers = "From: phpsmswhois@patrickg.net\r\n";
$headers .= "Reply-To: phpsmswhois@patrickg.net\r\n";
$headers .= "Return-Path: phpsmswhois@patrickg.net\r\n";
mail($to, $subject, $body, $headers);
?>
This is where we setup our email addresses for our message, and send the message out. This part is pretty self explanatory. For some hosts, setting the from and reply-to email addresses is enough, others require to also pass a return-path email. Best to play it safe, You can always remove this if you want/need. Also make sure you have execution privileges on the script, in most cases, this means you’ll need to chmod the php file to at least 744
So there you have it, your own email piping script. The possibilities of what to do with this are entirely on your shoulders, but one idea I had, was creating a way I could send a text message to check a domain’s availability. The full script is below. Happy piping.
#!/usr/bin/php -q
<?php
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
$lines = explode("\n", $email);
$from = "";
$subject = "";
$headers = "";
$message = "";
$splittingheaders = true;
for ($i=0; $i < count($lines); $i++) {
if ($splittingheaders) {
$headers .= $lines[$i]."\n";
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
} else {
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
$splittingheaders = false;
}
}
$to = "$from";
$subject = "This is a piped email.";
$body = "$from just sent an email with the subject "$subject", and the message "$message"";
$headers = "From: phpsmswhois@patrickg.net\r\n";
$headers .= "Reply-To: phpsmswhois@patrickg.net\r\n";
$headers .= "Return-Path: phpsmswhois@patrickg.net\r\n";
mail($to, $subject, $body, $headers);
?>

Comments are closed.