Simple Email Piping Script
#!/usr/bin/php -q
<?
$email_msg = ''; // the content of the email that is being piped
$email_addr = 'user2@bamboohillskl.com, user3@bamboohillskl.com'; // where the email will be sent
$subject = ''; // the subject of the email being sent
// open a handle to the email
$fh = fopen("php://stdin", "r");
// read through the email until the end
while (!feof($fh)){
$email_msg .= fread($fh, 1024);
}
fclose($fh);
$lines = explode("\n", $email_msg);
$from = "";
$to = "";
$subject1 = "";
$headers = "";
$is_header= true;
//loop through each line
for ($i=0; $i < count($lines); $i++) {
if ($is_header) {
// hear information. instead of main message body, all other information are here.
$headers .= $lines[$i]."\n";
// Split out the subject portion
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject1 = $matches[1];
}
//Split out the recipient information portion
if (preg_match("/^To: (.*)/", $lines[$i], $matches)) {
$to = $matches[1];
}
//Split out the sender information portion
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
} else {
// content/main message body information
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
// empty line, header section has ended
$is_header = false;
}
}
$from .= " with subject ' $subject1 ' .\r\n";
$subject = "New Email for $to !";
// send a copy of the email to your account
mail($email_addr, $subject, "Email received from ".$from);
?>
Last updated
Was this helpful?