* 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,109 +637,108 @@ class mail_hooks
*/ */
static function notification_check_mailbox() static function notification_check_mailbox()
{ {
$recipient = (object)$GLOBALS['egw']->accounts->read($GLOBALS['egw_info']['user']['account_id']); $accountsToSearch = emailadmin_account::search($only_current_user=true, $just_name=true);
foreach($accountsToSearch as $acc_id => $identity_name)
$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)
{ {
$folders2notify[$acc_id] = emailadmin_notifications::read($acc_id,$GLOBALS['egw_info']['user']['account_id']); //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);
} }
if(count($notify_folders) == 0) {
return true; //no folders configured for notifying - exit
}
$activeProfile = (int)$GLOBALS['egw_info']['user']['preferences']['mail']['ActiveProfileID'];
//error_log(__METHOD__.__LINE__.' (user: '.$recipient->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
{
$bomail->openConnection($activeProfile);
} 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!');
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); $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);
//$notified_mail_uidsCache = array(); //error_log(__METHOD__.__LINE__.array2string($notified_mail_uidsCache));
$recent_messages = array();
$folder_status = array(); foreach ($folders2notify as $nFKey =>$notifyfolders)
foreach($notify_folders as $id=>$notify_folder) { {
if (empty($notify_folder)) continue; $currentRecipient = (object)$GLOBALS['egw']->accounts->read(($notifyfolders['notify_account_id']?$notifyfolders['notify_account_id']:$GLOBALS['egw_info']['user']['account_id']));
if(!is_array($notified_mail_uidsCache[$activeProfile][$notify_folder])) { //error_log(__METHOD__.__LINE__.' '.$nFKey.' =>'.array2string($notifyfolders));
$notified_mail_uidsCache[$activeProfile][$notify_folder] = array(); $notify_folders = $notifyfolders['notify_folders'];
if(count($notify_folders) == 0) {
continue; //no folders configured for notifying
} }
$folder_status[$notify_folder] = $bomail->getFolderStatus($notify_folder); $activeProfile = $nFKey;
$cutoffdate = time(); //error_log(__METHOD__.__LINE__.' (user: '.$currentRecipient->account_lid.') Active Profile:'.$activeProfile);
$cutoffdate = $cutoffdate - (60*60*24*14); // last 14 days $bomail = mail_bo::getInstance(false, $activeProfile);
$_filter = array('status'=>array('UNSEEN','UNDELETED'),'type'=>"SINCE",'string'=> date("d-M-Y", $cutoffdate)); // buffer mail sessiondata, as they are needed for information exchange by the app itself
//error_log(__METHOD__.__LINE__.' (user: '.$recipient->account_lid.') Mailbox:'.$notify_folder.' filter:'.array2string($_filter)); //$bufferFMailSession = $bomail->sessionData;
// $_folderName, $_startMessage, $_numberOfMessages, $_sort, $_reverse, $_filter, $_thisUIDOnly=null, $_cacheResult=true try
$headers = $bomail->getHeaders($notify_folder, 1, 999, 0, true, $_filter,null,false); {
if(is_array($headers['header']) && count($headers['header']) > 0) { $bomail->openConnection($activeProfile);
foreach($headers['header'] as $id=>$header) { } catch (Exception $e) {
//error_log(__METHOD__.__LINE__.' Found Message:'.$header['uid'].' Subject:'.$header['subject']); // TODO: This is ugly. Log a bit nicer!
// check if unseen mail has already been notified $error = $e->getMessage();
if(!in_array($header['uid'], $notified_mail_uidsCache[$activeProfile][$notify_folder])) { error_log(__METHOD__.__LINE__.' # '.' (user: '.$currentRecipient->account_lid.'): cannot connect to mailbox with Profile:'.$activeProfile.'. Please check your prefs!');
// got a REAL recent message if (!empty($error)) error_log(__METHOD__.__LINE__.' # '.$error);
$header['folder'] = $notify_folder; error_log(__METHOD__.__LINE__.' # Instance='.$GLOBALS['egw_info']['user']['domain'].', User='.$GLOBALS['egw_info']['user']['account_lid']);
$header['folder_display_name'] = $folder_status[$notify_folder]['displayName']; return false; // cannot connect to mailbox
$header['folder_base64'] = base64_encode($notify_folder); }
$recent_messages[] = $header; //error_log(__METHOD__.__LINE__.array2string($notified_mail_uidsCache[$activeProfile][$notify_folder]));
} //$notified_mail_uidsCache = array();
$recent_messages = array();
$folder_status = array();
foreach($notify_folders as $id=>$notify_folder) {
if (empty($notify_folder)) continue;
if(!is_array($notified_mail_uidsCache[$activeProfile][$notify_folder])) {
$notified_mail_uidsCache[$activeProfile][$notify_folder] = array();
}
$folder_status[$notify_folder] = $bomail->getFolderStatus($notify_folder);
$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: '.$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
$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'];
$header['folder_base64'] = base64_encode($notify_folder);
$recent_messages[] = $header;
}
}
} }
} }
} //error_log(__METHOD__.__LINE__.' Found Messages for Profile'.$activeProfile.':'.array2string($recent_messages).'<->'.array2string($notified_mail_uidsCache[$activeProfile]));
//error_log(__METHOD__.__LINE__.' Found Messages for Profile'.$activeProfile.':'.array2string($recent_messages).'<->'.array2string($notified_mail_uidsCache[$activeProfile])); // restore the mail session data, as they are needed by the app itself
// restore the mail session data, as they are needed by the app itself if(count($recent_messages) > 0) {
if(count($recent_messages) > 0) { // create notify message
// create notify message $notification_subject = lang("You've got new mail").':'.$accountsToSearchArray[$activeProfile];
$notification_subject = lang("You've got new mail"); $values = array();
$values = array(); $values[] = array(); // content array starts at index 1
$values[] = array(); // content array starts at index 1 foreach($recent_messages as $id=>$recent_message) {
foreach($recent_messages as $id=>$recent_message) { error_log(__METHOD__.__LINE__.' Found Message for Profile '.$activeProfile.':'.array2string($recent_message));
error_log(__METHOD__.__LINE__.' Found Message for Profile '.$activeProfile.':'.array2string($recent_message)); $values[] = array(
$values[] = array( 'mail_uid' => $recent_message['uid'],
'mail_uid' => $recent_message['uid'], 'mail_folder' => $recent_message['folder_display_name'],
'mail_folder' => $recent_message['folder_display_name'], 'mail_folder_base64' => $recent_message['folder_base64'],
'mail_folder_base64' => $recent_message['folder_base64'], 'mail_subject' => $recent_message['subject'],
'mail_subject' => $recent_message['subject'], 'mail_from' => !empty($recent_message['sender_name']) ? $recent_message['sender_name'] : $recent_message['sender_address'],
'mail_from' => !empty($recent_message['sender_name']) ? $recent_message['sender_name'] : $recent_message['sender_address'], 'mail_received' => $recent_message['date'],
'mail_received' => $recent_message['date'], );
); // save notification status
// save notification status $notified_mail_uidsCache[$activeProfile][$recent_message['folder']][] = mail_ui::generateRowID($activeProfile, $recent_message['folder'], $recent_message['uid'], $_prependApp=false);
$notified_mail_uidsCache[$activeProfile][$recent_message['folder']][] = $recent_message['uid']; }
// 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($currentRecipient->account_id));
$notification->set_message($notification_message);
//$notification->set_popupmessage($notification_message);
$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);
// 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_message($notification_message);
//$notification->set_popupmessage($notification_message);
$notification->set_sender($recipient->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; return true;
} }