<?
// Debug Flag
$debug = False;
$reportemail = '[email protected]';
$presidentemail = '[email protected]';
$webmasteremail = '[email protected]';
//$pwd = 'password';
//check for new messages
$mailbox = imap_open("{localhost/pop3:110}INBOX", '[email protected]', 'password');
if ($mailbox)
{
// Check messages
$check = imap_check($mailbox);
if ($debug) {
print("<HTML>");
print(" <HEAD>");
print(" <TITLE>imap_check</TITLE>");
print(" </HEAD>");
print(" <BODY>");
print(" <PRE>");
print(" Date most recent message : " . $check->Date);
print(" <BR>");
print(" Connection type : " . $check->Driver);
print(" <BR>");
print(" Name of the mailbox : " . $check->Mailbox);
print(" <BR>");
print(" Number of messages : " . $check->Nmsgs);
print(" <BR>");
print(" Number of recent messages : " . $check->Recent);
print(" <BR>");
print(" </PRE>");
}
// Gonna have to loop and find the new messages that
// we have not yet parsed. store the last message num
// in a text file.
// Open the counter file and get the last message checked.
// then loop through the messages beginning at the last
// message checked + 1.
$lastmsg = file_get_contents('msgcounter.dat') + 1;
// Get header for message(s)
for ($index = $lastmsg-1; $index <= $check->Nmsgs; $index++ ) {
$games = array();
$recips = array();
$header = imap_header($mailbox, $index);
if ($debug) {
print(" <HR><PRE>");
print(" Header Date : " . $header->Date . "<BR>");
$toname = $header->to[0]->personal;
$toaddress = "<".$header->to[0]->mailbox."@".$header->to[0]->host.">";
print("Header To : " . $toname." ".$toaddress . "<BR>");
$fromname = $header->from[0]->personal;
$fromaddress = "<".$header->from[0]->mailbox."@".$header->from[0]->host.">";
print("Header From : " . $fromname." ".$fromaddress . "<BR>");
print(" Header cc : " . $header->cc . "<BR>");
print(" Header ReplyTo : " . $header->ReplyTo . "<BR>");
print(" Header Subject : " . $header->Subject . "<BR></PRE>");
print(" <PRE>");
print( htmlspecialchars(quoted_printable_decode(imap_body($mailbox,$index))));
print(" </PRE></BODY></HTML>");
} else {
// Score-221-4-0-42
list($sub, $gamenum, $visitorscore, $homescore, $teamnum) = split("-", $header->Subject );
// Now update the games table ONLY if the vaiable $sub = 'Score'
if (strcasecmp($sub, 'SCORE') == 0) {
$sql = "update games \n";
$sql .= "set visitorscore =". $visitorscore.", homescore = ".$homescore. "\n";
$sql .= "where gamenum = ".$gamenum." and \n";
$sql .= " (\n";
$sql .= " (homeid = ".$teamnum.")\n";
$sql .= " or \n";
$sql .= " (visitorid = ".$teamnum.")\n";
$sql .= " )\n";
// Do DB stuff here...
print($sql);
// Add to $games array for later reporting
array_push($games,array($gamenum, $visitorscore, $homescore));
// add the sender of this email to $recips array
// for later sending confirmation email
$fromname = $header->from[0]->personal;
$fromaddress = "<".$header->from[0]->mailbox."@".$header->from[0]->host.">";
array_push($recips, $fromname." ".$fromaddress);
}
imap_close($mailbox);
// Store the last checked message number to the counter file.
print($index."\n");
//file_put_contents('msgcounter.dat', $index);
if (!$handle = fopen('msgcounter.dat', 'w')) {
echo "Cannot open file ('msgcounter.dat')";
exit;
}
// Write $index to our opened file.
if (fwrite($handle, $index) === FALSE) {
echo "Cannot write to file ('msgcounter.dat')";
exit;
}
fclose($handle);
}
}
// Now send confirmations to the Message Sender, League President and the Webmaster
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-MSMail-Priority: Normal\n";
$headers .= "X-Mailer: php\n";
$headers .= "From: \"Automated Scores\" <".$reportemail.">\n";
$message = "The score for these game(s) have been reported:\n";
for ($i = 0; $i <= count($games); $i++) {
$message .= print_r($games)."\n";
}
mail($presidentemail, 'Score Reported' + $header->Subject, $message, $headers);
mail($webmasteremail, 'Score Reported' + $header->Subject, $message, $headers);
for ($i = 0; $i <= count($recips); $i++) {
mail($recips[$i], 'Score Reported' + $header->Subject, $message, $headers);
}
} else {
print("Can't open mailbox");
}
?>
|