Ok, I've been running this script for the last 4 or 5 hours and everything seems to be doing what I want.
-Define your pop3 email accounts you want monitored at the top of the script.
-SSH into the core and place the scripts where ever you want on the core. I'd suggest the home directory you land in.
-Execute the program in the background with: php chkmail.php & or just run it and monitor the action: php chkmail.php
The script will check your pop3 accounts at whatever interval you set (default is 5 minutes). The script is smart enough to keep a running tally of the message id's it has already processed, so you get one warning about a new message, instead of repeated notices about messages you've already been alerted to. Therefore, whatever directory you put these scripts into must have write access. If you check your mail and clean out your inbox the script will delete the tally and start fresh.
All this does is simply flash a message on your screen informing you of new incoming email messages. I find it handy to monitor my various email accounts while watching video or tv in the living-room or basement. I can then decide if I want/need to pause the video and read/reply to the email message.
You need to get the pop3.class.inc file from this location:
http://www.phpclasses.org/browse/package/1120.htmlJust scroll to the bottom where you can click on DOWNLOAD and save it. There's also a version available for PHP5.
And now here's some code I've assembled:
<?php
// A little php script to check for new mail messages and sent a display_alert
// command to alert LinuxMCE users of new email messages
// Put this script and the pop3.class.inc somewhere. Data files will be created
// in this directory to store data about incoming messages.
// To run it: php chkmail.php
// and press Ctrl-C to stop. Or make it a background job:
// php chkmail.php &
//
// -------------------------------------------------------------
// Add your mail account info to the $mail_accounts array
// Separate your different accounts with opening/closing quotes
// Separeate your account info with a semi-colon (;)
// $mail_accounts = array("pop3.servername;pop.usename;pop3.password")
// Add as many accounts as you like
// $mail_accounts = array("pop3.servername;pop.usename;pop3.password","pop3.servername2;pop.usename2;pop3.password2")
//
$mail_accounts = array("pop3.server1;username1;password1","pop3.server2;username2;password2");
$ignore_subjects = array("Cron");
// # of seconds between mail checks. 300 = 5 mins.
$waitfor = 300;
//
// You shouldn't have to edit anything below
// -------------------------------------------------------------
require_once('pop3.class.inc');
$pop3 = new POP3;
while (1) {
foreach ($mail_accounts as $account) {
list($mailserver,$username,$password) = explode(";", $account);
// Read in data file for this account
$file_name = $mailserver . "-" . $username . ".cfg";
$msgids = read_file($file_name);
if ($msgids == false) {
// That's ok, no previous data
$ids = array();
$orig_count = 0;
} else {
$ids = explode("\n", $msgids);
$tally = count($ids) - 1;
for ($i = 0; $i < $tally; $i++) {
// Want to strip carraige returns and line feeds so our array
// comparisons later match correctly
$ids[$i] = ereg_replace("/\n\r|\r\n/", "", $ids[$i]);
}
$orig_count = (count($ids));
}
// Connect to mail server
$do = $pop3->connect($mailserver);
if ($do == false) {
echo $pop3->error;
continue;
}
// Login to your inbox
$do = $pop3->login($username, $password);
if ($do == false) {
echo $pop3->error;
continue;
}
// Get office status
$status = $pop3->get_office_status();
if ($status == false) {
echo $pop3->error;
continue;
}
$count = $status['count_mails'];
for ($i = 1; $i <= $count; $i++) {
$email = $pop3->get_mail($i);
$email = parse_email($email);
if ($email == false) {
echo $pop3->error;
continue;
}
// Check to see if the Message-ID is in our ids array.
// Only announce if it's a new message and NOT in the existing array
$testid = trim($email['headers']['Message-ID']);
$testid = get_simple_email($testid);
if (empty($testid) || $testid == " ") {
// Try different spelling
$testid = trim($email['headers']['Message-Id']);
$testid = get_simple_email($testid);
}
if (empty($testid)) {
print_r($email[headers]);
$testid = time();
}
if (!in_array($testid, $ids)) {
// add this one to the ids array
if (count($ids)==0) {
$ids = array($testid);
} else {
array_push($ids, $testid);
}
// echo a few things to the terminal for monitoring purposes
echo "Message-ID: [$testid]\n";
$efrom = $email['headers']['From'];
$efrom = get_simple_email($efrom);
echo date("Y-m-d H:i:s") . " New messages from: $efrom\n";
echo 'Subject: ' . $email['headers']['Subject'] . "\n\n";
// send message to dcerouter if subject isn't in ignore list
$found = 0;
foreach ($ignore_subjects as $subj) {
if (eregi($subj, $email['headers']['Subject'])) {
$found = 1;
break;
}
}
if (!$found) {
$message = "Email from $efrom regarding " . $email['headers']['Subject'];
exec("/usr/pluto/bin/MessageSend localhost 0 -305 1 809 9 \"$message\" 70 \"\" 182 10 251 0");
// Let the pop up show for multiple messages
sleep(10);
}
}
}
if ($count == '0') {
echo date("Y-m-d H:i:s") . " There are no new e-mails\n";
// This is a good place to clear out the data file since the mailbox is now empty
if (file_exists($file_name)) {
@unlink($file_name);
}
}
// See if there's been any additions to the ids array
// and if so, write back the new info
$new_count = (count($ids));
if ($orig_count < $new_count) {
write_file($file_name, $ids);
}
$pop3->close();
}
unset($msgids);
unset($ids);
sleep($waitfor);
}
function parse_email ($email) {
// Split header and message
$header = array();
$message = array();
$is_header = true;
foreach ($email as $line) {
if ($line == '<HEADER> ' . "\r\n") continue;
if ($line == '<MESSAGE> ' . "\r\n") continue;
if ($line == '</MESSAGE> ' . "\r\n") continue;
if ($line == '</HEADER> ' . "\r\n") { $is_header = false; continue; }
if ($is_header == true) {
$header[] = $line;
} else {
$message[] = $line;
}
}
// Parse headers
$headers = array();
foreach ($header as $line) {
$colon_pos = strpos($line, ':');
$space_pos = strpos($line, ' ');
if ($colon_pos === false OR $space_pos < $colon_pos) {
// attach to previous
$previous .= "\r\n" . $line;
continue;
}
// Get key
$key = substr($line, 0, $colon_pos);
// Get value
$value = substr($line, $colon_pos+2);
$headers[$key] = $value;
$previous =& $headers[$key];
}
// Parse message
$message = implode('', $message);
// Return array
$email = array();
$email['message'] = $message;
$email['headers'] = $headers;
return $email;
}
function get_simple_email($str) {
$open = strpos($str, "<");
$close = strpos($str, ">");
if (($open === false) && ($close === false)) {
return $str;
}
return substr($str, $open + 1, $close - $open - 1);
}
function read_file($name) {
$contents = false;
if (file_exists($name)) {
if ($fd = @fopen($name, "r")) {
$contents = fread($fd, filesize($name));
fclose($fd);
}
}
return $contents;
}
function write_file($name, $data) {
if ($fd = @fopen($name, "w")) {
foreach ($data as $line) {
$line = ereg_replace("/\n\r|\r\n/", "", $line);
if (!empty($line)) {
fwrite($fd, $line . "\n");
}
}
fclose($fd);
return true;
} else {
return false;
}
}
?>
Save this as whatever you want. I called it chkmail.php
That's it!