[SOLVED] php reading from email
Posted: Sat Aug 21, 2010 10:21 am
I've set up an email address on my server with a subject filter which changes the mail folder the email is stored to, as well as piping the email to a PHP file, should the subject be equal to "afjk89". The purpose of the PHP file is to save the message body to a text file in the same directory as the PHP.
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:
Any help on this would be appreciated as I am quite new to PHP, and currently I am completely stumped as to what to try next 
[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:
Seeing as I wanted this in order to update parts of my website quickly I added a function at the end for ease-of-use, so you just have to include this file on the page that needs to be editable and then call the function emRe() in the desired location. the purpose of nl2br() makes it so you don't have to write <br /> in your email where you want a new line - very handy indeed!
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.
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.