Click to See Complete Forum and Search --> : importing email to mysql


metrouni
07-19-2008, 09:26 PM
Hello,

I am trying to have emails that are piped to a program get INSERTed into a mySQL database. Below is the code I have, but the emails keep getting kicked back.

Any help would be great! Thank you!


#!/ramdisk/bin/php5
<?php

// 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;
}
}

// Now that we have the message, let's send it to the database

$user_name = "metroun1_XXX";
$password = "XXX";
$database = "metroun1_olam";
$server = "localhost";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
$date = date(ymdc);

if ($db_found) {

$sql = "INSERT INTO noc_mail(downloaded, from, subject, headers, message, status) VALUES('$date', '$from', '$subject', '$headers', '$message', 'NEW')";

$result = mysql_query($sql);

}
if($result) {

}else {

die();
}


mysql_close($db_handle);



?>



The mailer demons message:


his message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

pipe to |/ramdisk/bin/php5/ home/metroun1/public_html/pros/2.0/incoming_mail/process_mail.php
generated by support@mudsl.com
local delivery failed

chazzy
07-20-2008, 09:48 AM
check the $PATH variable. php5 should not have a slash after it.

metrouni
09-29-2008, 07:03 PM
I checked that, and it's still not working. Is there an easier/quicker way to accomplish this???

ariell
09-29-2008, 07:25 PM
Hi,

I think the fastest way to ask the SERVER, something like:

...
if ($db_found) { // query connected to handle AND db

$sql = "INSERT INTO noc_mail(downloaded, from, subject, headers, message, status) VALUES('$date', '$from', '$subject', '$headers', '$message', 'NEW')";
$result = mysql_query($sql);
} // query connected to handle AND db
else
echo('can\'t insert, error is ['.mysql_error($dbhandle).']<br>');
...

Shana tova,
ariell.

opifex
09-29-2008, 08:54 PM
check how your php and mail server are set up. some hosting providers DO NOT have everything enabled by default and you can enable it yourself.

but... an alternative is to send the mail AND post the data to the db separately directly from the form. might be a simpler solution.