<?php
/**
 * E-mail notification script.
 *
 * It checks your pop3/imap e-mail account for new messages and notifies you
 * by sending an SMS message on a mobile number.
 *
 * @author Attila Szabo (http://w3net.eu)
 */
error_reporting(E_ALL E_STRICT);
if (
function_exists('date_default_timezone_set')){
    
date_default_timezone_set('Europe/Paris');
}

// settings
$email_username 'john';
$email_password 'xxxxxxxxxxxxxx';
$email_account  '{mail.t-com.sk/pop3}INBOX'// change this according your email service provider
$mobilePhone    '421000000000'// sends notification to this number (your mobile phone number)
$senders_filter = array('/<*@(w3net\.eu|microsoft\.com|gmail\.com)>/'); // Important to customize !!
$dateFrom mktime(000date("m"), date("d")-1date('Y')); // yesterday
$dateTo mktime(235959date("m"), date("d")-1date('Y')); // yesterday
$errorsSendToEmail ''// optional; Error messages are sent to this e-mail.

/* GMAIL *//*
$email_username = 'w3net.xxxxxxxxxxx@gmail.com';
$email_password = 'xxxxxxxxxxxxxx';
$email_account  = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX'; // no change is needed here
$mobilePhone    = '421000000000'; // sends notification to this number (your mobile phone number)
$senders_filter = array('/<*@(w3net\.eu|gmail\.com|facebook\.com)>/');
$dateFrom = mktime(0, 0, 0, date("m"), date("d")-1, date('Y')); // yesterday
$dateTo = mktime(23, 59, 59, date("m"), date("d")-1, date('Y')); // yesterday
$errorsSendToEmail = ''; // optional; Error messages are sent to this e-mail.
*/

$a FetchEmails($dateFrom,$dateTo$senders_filter);
if (
count($a)){

    include(
'services/smsgateway/class.Transport.php');
    include(
'services/smsgateway/class.SmsGateway.php');

    try{
        
SendReport(ProduceReport($a));
    }catch(
Exception $e){
        if (isset(
$errorsSendToEmail) && !empty($errorsSendToEmail)){
            
mail($errorsSendToEmail'Email notifier PHP script - error occured'$e->getMessage());
        }
    }

}else{
    die(
'no messages'); // nothing to do
}




/**
 * Fetches e-mails delivered within the given time frame from pop3 account.
 * 
 * Returns e-mails received (array of objects describing one message header each)
 * in the given time frame. {@link http://php.net/manual/en/function.imap-fetch-overview.php imap_fetch_overview}.
 * 
 * @param int $dateFrom Unix timestamp
 * @param int $dateTo Unix timestamp
 * @param array $sendersFilter array of PERL Regular expressions.
 * Returns emails whose sender's address matches one of the regular expression.
 * @return array
 */
function FetchEmails($dateFrom$dateTo, Array $sendersFilter){
    
$ret = array();

    
$rs imap_open($GLOBALS['email_account'], $GLOBALS['email_username'], $GLOBALS['email_password'])
        or die(
imap_last_error());


    if (
is_resource($rs) && imap_num_msg($rs) > 0){
        
$MC imap_check($rs);

        
// Fetch an overview for all messages in INBOX
        
$colMsgs imap_fetch_overview($rs,"1:{$MC->Nmsgs}",0);

        
// filter out messages received yesterday
        
foreach ($colMsgs as $overview) {

            
$dateReceived strtotime($overview->date);
            if (
$dateReceived $dateFrom &&
                
$dateReceived $dateTo){

                
$header imap_fetchheader($rs$overview->msgno);
                foreach(
$sendersFilter as $pattern){
                    if (
preg_match($pattern,$header)){
                        
$ret[] = $overview;
                        break;
                    }
                }

            }
        }

        
imap_close($rs);
    }

    return 
$ret;
}

/**
 * Produces SMS message to the user to be sent
 * @param array $aMsgs Array of objects {@link http://php.net/manual/en/function.imap-fetch-overview.php imap_fetch_overview}.
 * @return string
 */
function ProduceReport($aMsgs){
    
$ret "Emails: \n\n";

    foreach (
$aMsgs as $overview) {
        
$dateReceived strtotime($overview->date);
        
$ret .= $overview->from  ." : "$overview->subject " ("date('G:i:s',$dateReceived) .")\n";
    }

    return 
$ret;
}

/**
 * Sends SMS notification using SMS gateway class
 *
 * @param string $msg The message to send (max. 160 charcters)
 * @throws Exception on sending error
 */
function SendReport($msg){

    try{

        
$oSmsGateway = new SmsGateway();
        
$oSmsGateway->SendSingleSms($GLOBALS['mobilePhone'], $msg);
        unset(
$oSmsGateway);

    }catch(
TransportException $e){
        throw new 
Exception('SMS proxy error: '$e->getMessage());
    }catch(
Excption $e){
        throw new 
Exception('Unexpected error: '$e->getMessage());
    }
}