I got the folder-redirect to work once, saving the email to a different folder than my inbox, but have not been able to get it to create the text file. I moved the PHP file from /home/naebeth/email_test to /home/naebeth/public_html/cgi-bin in case that would help, but it didn't, and since moving it back I don't seem to be receiving the new emails at all (in inbox or my custom folder) as well as still not generating the text file.
I have given /home/naebeth/email_test permissions 777 and the PHP file 755.
The email filter pipes via this line:
usr/bin/php-cgi /home/naebeth/email_test/email.php
And this is the contents of my php file:
Code: Select all
#!/usr/bin/php
<?php
$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;
}
}
if $subject = "afjk89" {
$fh = fopen("email.txt",'w');
fwrite($fh,$message);
fclose($fh);
}
?>[EDIT]
I've got it all working now, here's is the pipe command:
|/usr/bin/php-cgi /home/naebeth/public_html/email_test/email.php
And here's the PHP file:
Code: Select all
<?php
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
$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;
}
}
if ($subject == "afjk89") {
unlink("email.txt");
$fh = fopen("email.txt",'w');
fwrite($fh,$message);
fclose($fh);
}
function emRe(){
$MessageText = fopen("email.txt",'r');
echo nl2br(fread($MessageText,1024));
}
?>However, everything above if ($subject == "afjk89") can be used universally to access any of the main headers and the message of plain-text emails.
When I devise a way of using HTML emails (forgot to change it to plain text once during testing and found a shed load of extra information that was useless to me) I'll post it as a reply here.
And then the next step will be making it so you can use it for updating media content, primarily images.