Author Topic: How to get a message in that pop-up status window  (Read 4058 times)

freymann

  • Douchebag
  • Guru
  • *
  • Posts: 380
    • View Profile
Re: How to get a message in that pop-up status window
« Reply #15 on: October 23, 2008, 04:31:23 am »
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.html

Just 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:

Code: [Select]
<?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($line0$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($fdfilesize($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!


« Last Edit: November 05, 2008, 07:19:21 pm by freymann »

chriss

  • Veteran
  • ***
  • Posts: 140
    • View Profile
Re: How to get a message in that pop-up status window
« Reply #16 on: October 23, 2008, 09:24:05 am »
Wow, nice work. Can you put that on the wiki?  8)

totallymaxed

  • LinuxMCE God
  • ****
  • Posts: 4360
  • Smart Home Consulting
    • View Profile
    • Dianemo - at home with technology
Re: How to get a message in that pop-up status window
« Reply #17 on: October 23, 2008, 12:51:47 pm »
I just had the program running and was watching a video on the living-room MD which uses UI2 and the pop ups appear fine on that set. The pop ups don't appear (when watching video) on my notebook MD which uses UI1.

That's not an issue for me, as I wanted to be aware of new emails when at the living-room or basement stations, so onwards and upwards.



Is your notebook running the Windows Orbiter or is it pxe booted as a full MD?

Andrew
Andy Herron,
Convergent Home Technologies Ltd
United Kingdom

Read My Blog; http://ellipticalcurve.com

Contact me for Smart Home consulting advice here;
@herron on Twitter, totallymaxed+consulting@gmail.com via email or PM me here.

Get a Dianemo S License: http://forum.linuxmce.org/index.php?topic=8880.0
iOS Orbiter: http://wiki.linuxmce.org/index.php/Dianemo_iOS_Orbiter
Follow us on Facebook: https://www.facebook.com/pages/Dianemo-Home-Automation/226019387454465

Sales & Info:
http://www.dianemo.co.uk

freymann

  • Douchebag
  • Guru
  • *
  • Posts: 380
    • View Profile
Re: How to get a message in that pop-up status window
« Reply #18 on: October 23, 2008, 01:45:39 pm »
I just had the program running and was watching a video on the living-room MD which uses UI2 and the pop ups appear fine on that set. The pop ups don't appear (when watching video) on my notebook MD which uses UI1.

That's not an issue for me, as I wanted to be aware of new emails when at the living-room or basement stations, so onwards and upwards.



Is your notebook running the Windows Orbiter or is it pxe booted as a full MD?


Hi Andrew. It boots over the network and is a full MD.

It uses UI1. I do see the message pop-ups when at the menu but not when watching video.

freymann

  • Douchebag
  • Guru
  • *
  • Posts: 380
    • View Profile
Re: How to get a message in that pop-up status window
« Reply #19 on: October 23, 2008, 01:46:30 pm »
Wow, nice work. Can you put that on the wiki?  8)

Thanks. Yes, I was going to do that after everybody has had a chance to check it out.

posde

  • Administrator
  • LinuxMCE God
  • *****
  • Posts: 2927
  • Wastes Life On LinuxMCE Since 2007
    • View Profile
    • My Home
Re: How to get a message in that pop-up status window
« Reply #20 on: October 24, 2008, 09:37:47 pm »
Could you put the needed pop3 php code someplace where one does not need to be registered?

rgds
Oliver

tschak909

  • LinuxMCE God
  • ****
  • Posts: 5495
  • DOES work for LinuxMCE.
    • View Profile
Re: How to get a message in that pop-up status window
« Reply #21 on: October 24, 2008, 09:40:46 pm »
it is worth noting, that eventually LinuxMCE will have a mail client of its own on the orbiter, and will thus provide email notifications on its own.

-Thom

freymann

  • Douchebag
  • Guru
  • *
  • Posts: 380
    • View Profile
Re: How to get a message in that pop-up status window
« Reply #22 on: October 24, 2008, 09:47:37 pm »
Could you put the needed pop3 php code someplace where one does not need to be registered?

 I wasn't registered. I just went to the bottom of the screen, clicked on the link for the needed include file and off it went.

 But yes, if you can't get from there, try it from here:

http://ww2.interpool.ca/files/pop3.class.inc

freymann

  • Douchebag
  • Guru
  • *
  • Posts: 380
    • View Profile
Re: How to get a message in that pop-up status window
« Reply #23 on: October 24, 2008, 09:48:24 pm »
it is worth noting, that eventually LinuxMCE will have a mail client of its own on the orbiter, and will thus provide email notifications on its own.

 Kewl! Until then this method is working great!


dlewis

  • Guru
  • ****
  • Posts: 401
    • View Profile
Re: How to get a message in that pop-up status window
« Reply #24 on: October 24, 2008, 10:08:36 pm »
freyman, create and article and post this to the wiki? Thanks!

freymann

  • Douchebag
  • Guru
  • *
  • Posts: 380
    • View Profile
Re: How to get a message in that pop-up status window
« Reply #25 on: October 24, 2008, 10:37:58 pm »
freyman, create and article and post this to the wiki? Thanks!

 Yes, I planned on doing that after I had used the thing for a few days. I updated my script included at the top of page 2 once, and that's the script I'm using to this day...

 So probably on Monday I'll create the wiki page for this.

freymann

  • Douchebag
  • Guru
  • *
  • Posts: 380
    • View Profile
Re: How to get a message in that pop-up status window
« Reply #26 on: October 27, 2008, 04:12:50 pm »
There is now a wiki page for this:

http://wiki.linuxmce.org/index.php/Display_Alert_Email_Notification

I added an array you can use to ignore messages with matching words in the subject line.