Well it doesnt seem to work my script will block itself from trying to readd an email that is allready in the db table but it doesnt add new emails.
here is the code as it currently stands:
<?
// requires the setting vars for conecting to mysql db and imap email acount
require 'conf.php';
// conects to imap server and gets the email
$link=imap_open($server,$email,$password);
$headers=imap_headers($link);
$numEmails = sizeof($headers);
echo "emails:<br>";
for($i = 1; $i < $numEmails+1; $i++){
$mailHeader = @imap_headerinfo($link, $i);
$id = $mailHeader->message_id;
$date = $mailHeader->date;
$from = $mailHeader->fromaddress;
$subject = strip_tags($mailHeader->subject);
$message = @imap_body($link, $i);
$message = substr($message, 0, 20);
$message = addslashes($message);
echo "<br>Id:$id<br>From:$from<br>Subject:$subject<br>Message:$message<br><br>";
// conects to the db to check for emails/add emails
$dbcnx = dbopen();
$sql = "SELECT id FROM sms_emails";
$result = mysql_query($sql) or die("Couldn't Execute Query, MySQL Said: " . mysql_error() ."");
while ($rows = mysql_fetch_array($result)){
$sql_id = $rows[id];
}
// stops or allows the emails to be added to the db
if ($id = $sql_id){
echo "duplacate email with id: $id not added<br><br>";
} else {
echo "email $id has been added<br>";
// insert the emails into the db table
$sql1 = mysql_query("INSERT INTO `sms_emails` ( `id` , `date` , `from` , `subject` , `message` ) VALUES ( '$id', '$date', '$from', '$subject', '$message' )") or die (mysql_error());
}
mysql_close($dbcnx);
}
?>
here is what it produces outputwise (with echo statements and run via a browser for debuging):
emails:
Id:<42EB6187.7010304@linuxbox>
From:mrweirdo
Subject:test1
Message:this is a test --
duplacate email with id:<42EB619D.7070303@linuxbox> not added
Id:<42EB619D.7070303@linuxbox>
From:mrweirdo
Subject:test2
Message:this is anouther tes
duplacate email with id:<42EB619D.7070303@linuxbox> not added
Id:<42EB8C86.6040900@linuxbox>
From:mrweirdo
Subject:test3
Message:this is test three o
duplacate email with id:<42EB619D.7070303@linuxbox> not added
Notice the common theme of duplacate email statement after each email grabed from the imap account is the same? hope someone has an idea as I'm now stuck
sql table dump:
CREATE TABLE `sms_emails` (
`id` varchar(60) NOT NULL default '',
`date` varchar(60) NOT NULL default '',
`from` varchar(60) NOT NULL default '',
`subject` varchar(60) NOT NULL default '',
`message` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) TYPE=MyISAM;
--
-- Dumping data for table `sms_emails`
--
INSERT INTO `sms_emails` VALUES ('<42EB6187.7010304@linuxbox>', 'Sat, 30 Jul 2005 04:16:23 -0700', 'mrweirdo <webmaster@linuxbox>', 'test1', 'this is a test\r\n\r\n--');
INSERT INTO `sms_emails` VALUES ('<42EB619D.7070303@linuxbox>', 'Sat, 30 Jul 2005 04:16:45 -0700', 'mrweirdo <webmaster@linuxbox>', 'test2', 'this is anouther tes');