* Mail: implement notification for incomming mails in folders if configured/set in mailaccountprofile

This commit is contained in:
Klaus Leithoff 2014-04-24 13:53:10 +00:00
parent f01cb48abf
commit caeb39417c

View File

@ -637,29 +637,27 @@ class mail_hooks
*/
static function notification_check_mailbox()
{
$recipient = (object)$GLOBALS['egw']->accounts->read($GLOBALS['egw_info']['user']['account_id']);
$prefs = new preferences($recipient->account_id);
$preferences = $prefs->read();
// TODO: no possibility to set that at this time; always use INBOX
if (empty($preferences['mail']['notify_folders'])) return true;//$preferences['mail']['notify_folders']='INBOX';
/* //error_log(__METHOD__.__LINE__.array2string($preferences['mail']['notify_folders']));
if(!isset($preferences['mail']['notify_folders'])||empty($preferences['mail']['notify_folders'])||$preferences['mail']['notify_folders']=='none') {
return true; //no pref set for notifying - exit
}
*/
foreach(emailadmin_account::search($only_current_user=true, $just_name=true) as $acc_id => $identity_name)
$accountsToSearch = emailadmin_account::search($only_current_user=true, $just_name=true);
foreach($accountsToSearch as $acc_id => $identity_name)
{
$folders2notify[$acc_id] = emailadmin_notifications::read($acc_id,$GLOBALS['egw_info']['user']['account_id']);
}
if(count($notify_folders) == 0) {
return true; //no folders configured for notifying - exit
//error_log(__METHOD__.__LINE__.' '.$acc_id.':'.$identity_name);
$folders2notify[$acc_id] = emailadmin_notifications::read($acc_id);// read all, even those set for acc_id 0 (folders for all acounts?)
$accountsToSearchArray[$acc_id] = str_replace(array('<','>'),array('[',']'),$identity_name);
}
$notified_mail_uidsCache = egw_cache::getCache(egw_cache::INSTANCE,'email','notified_mail_uids'.trim($GLOBALS['egw_info']['user']['account_id']),null,array(),$expiration=60*60*24*2);
//error_log(__METHOD__.__LINE__.array2string($notified_mail_uidsCache));
$activeProfile = (int)$GLOBALS['egw_info']['user']['preferences']['mail']['ActiveProfileID'];
//error_log(__METHOD__.__LINE__.' (user: '.$recipient->account_lid.') Active Profile:'.$activeProfile);
foreach ($folders2notify as $nFKey =>$notifyfolders)
{
$currentRecipient = (object)$GLOBALS['egw']->accounts->read(($notifyfolders['notify_account_id']?$notifyfolders['notify_account_id']:$GLOBALS['egw_info']['user']['account_id']));
//error_log(__METHOD__.__LINE__.' '.$nFKey.' =>'.array2string($notifyfolders));
$notify_folders = $notifyfolders['notify_folders'];
if(count($notify_folders) == 0) {
continue; //no folders configured for notifying
}
$activeProfile = $nFKey;
//error_log(__METHOD__.__LINE__.' (user: '.$currentRecipient->account_lid.') Active Profile:'.$activeProfile);
$bomail = mail_bo::getInstance(false, $activeProfile);
$activeProfile = $GLOBALS['egw_info']['user']['preferences']['mail']['ActiveProfileID'] = $bomail->profileID;
// buffer mail sessiondata, as they are needed for information exchange by the app itself
//$bufferFMailSession = $bomail->sessionData;
try
@ -668,13 +666,12 @@ class mail_hooks
} catch (Exception $e) {
// TODO: This is ugly. Log a bit nicer!
$error = $e->getMessage();
error_log(__METHOD__.__LINE__.' # '.' (user: '.$recipient->account_lid.'): cannot connect to mailbox with Profile:'.$activeProfile.'. Please check your prefs!');
error_log(__METHOD__.__LINE__.' # '.' (user: '.$currentRecipient->account_lid.'): cannot connect to mailbox with Profile:'.$activeProfile.'. Please check your prefs!');
if (!empty($error)) error_log(__METHOD__.__LINE__.' # '.$error);
error_log(__METHOD__.__LINE__.' # Instance='.$GLOBALS['egw_info']['user']['domain'].', User='.$GLOBALS['egw_info']['user']['account_lid']);
return false; // cannot connect to mailbox
}
$notified_mail_uidsCache = egw_cache::getCache(egw_cache::INSTANCE,'email','notified_mail_uids'.trim($GLOBALS['egw_info']['user']['account_id']),null,array(),$expiration=60*60*24*2);
//error_log(__METHOD__.__LINE__.array2string($notified_mail_uidsCache[$activeProfile][$notify_folder]));
//$notified_mail_uidsCache = array();
$recent_messages = array();
$folder_status = array();
@ -687,14 +684,15 @@ class mail_hooks
$cutoffdate = time();
$cutoffdate = $cutoffdate - (60*60*24*14); // last 14 days
$_filter = array('status'=>array('UNSEEN','UNDELETED'),'type'=>"SINCE",'string'=> date("d-M-Y", $cutoffdate));
//error_log(__METHOD__.__LINE__.' (user: '.$recipient->account_lid.') Mailbox:'.$notify_folder.' filter:'.array2string($_filter));
//error_log(__METHOD__.__LINE__.' (user: '.$currentRecipient->account_lid.') Mailbox:'.$notify_folder.' filter:'.array2string($_filter));
// $_folderName, $_startMessage, $_numberOfMessages, $_sort, $_reverse, $_filter, $_thisUIDOnly=null, $_cacheResult=true
$headers = $bomail->getHeaders($notify_folder, 1, 999, 0, true, $_filter,null,false);
if(is_array($headers['header']) && count($headers['header']) > 0) {
foreach($headers['header'] as $id=>$header) {
//error_log(__METHOD__.__LINE__.' Found Message:'.$header['uid'].' Subject:'.$header['subject']);
// check if unseen mail has already been notified
if(!in_array($header['uid'], $notified_mail_uidsCache[$activeProfile][$notify_folder])) {
$headerrowid = mail_ui::generateRowID($activeProfile, $notify_folder, $header['uid'], $_prependApp=false);
if(!in_array($headerrowid, $notified_mail_uidsCache[$activeProfile][$notify_folder])) {
// got a REAL recent message
$header['folder'] = $notify_folder;
$header['folder_display_name'] = $folder_status[$notify_folder]['displayName'];
@ -708,7 +706,7 @@ class mail_hooks
// restore the mail session data, as they are needed by the app itself
if(count($recent_messages) > 0) {
// create notify message
$notification_subject = lang("You've got new mail");
$notification_subject = lang("You've got new mail").':'.$accountsToSearchArray[$activeProfile];
$values = array();
$values[] = array(); // content array starts at index 1
foreach($recent_messages as $id=>$recent_message) {
@ -722,24 +720,25 @@ class mail_hooks
'mail_received' => $recent_message['date'],
);
// save notification status
$notified_mail_uidsCache[$activeProfile][$recent_message['folder']][] = $recent_message['uid'];
$notified_mail_uidsCache[$activeProfile][$recent_message['folder']][] = mail_ui::generateRowID($activeProfile, $recent_message['folder'], $recent_message['uid'], $_prependApp=false);
}
// create etemplate
$tpl = new etemplate('mail.checkmailbox');
$notification_message = $tpl->exec(false, $values, array(), array(), array(), 1);
//error_log(__METHOD__.__LINE__.array2string($notification_message));
// send notification
$notification = new notifications();
$notification->set_receivers(array($recipient->account_id));
$notification->set_receivers(array($currentRecipient->account_id));
$notification->set_message($notification_message);
//$notification->set_popupmessage($notification_message);
$notification->set_sender($recipient->account_id);
$notification->set_sender($currentRecipient->account_id);
$notification->set_subject($notification_subject);
$notification->set_skip_backends(array('email'));
$notification->send();
}
egw_cache::setCache(egw_cache::INSTANCE,'email','notified_mail_uids'.trim($GLOBALS['egw_info']['user']['account_id']),$notified_mail_uidsCache, $expiration=60*60*24*2);
}
//error_log(__METHOD__.__LINE__.array2string($notified_mail_uidsCache));
return true;
}