* Mail: skip accounts with no imap server set for notification and foldertree

This commit is contained in:
Klaus Leithoff 2014-04-25 09:42:18 +00:00
parent c912849bd5
commit b69e147247
2 changed files with 115 additions and 86 deletions

View File

@ -637,8 +637,9 @@ class mail_hooks
*/
static function notification_check_mailbox()
{
$accountsToSearch = emailadmin_account::search($only_current_user=true, $just_name=true);
foreach($accountsToSearch as $acc_id => $identity_name)
$accountsToSearchObj = emailadmin_account::search($only_current_user=true, $just_name=true);
foreach($accountsToSearchObj as $acc_id => $identity_name)
{
//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?)
@ -649,94 +650,112 @@ class mail_hooks
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);
// 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: '.$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
}
//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();
$currentRecipient = (object)$GLOBALS['egw']->accounts->read(($notifyfolders['notify_account_id']?$notifyfolders['notify_account_id']:$GLOBALS['egw_info']['user']['account_id']));
$notify_folders = $notifyfolders['notify_folders'];
if(count($notify_folders) == 0) {
continue; //no folders configured for notifying
}
$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__.' '.$nFKey.' =>'.array2string($notifyfolders));
$activeProfile = $nFKey;
//error_log(__METHOD__.__LINE__.' (user: '.$currentRecipient->account_lid.') Active Profile:'.$activeProfile);
try
{
$bomail = mail_bo::getInstance(false, $activeProfile,false);
} catch (Exception $e)
{
error_log(__METHOD__.__LINE__.' (user: '.$currentRecipient->account_lid.') notification for Profile:'.$activeProfile.' failed.'.$e->getMessage());
continue; //fail silently
}
/*
error_log(__METHOD__.__LINE__.' '.$nFKey.' =>'.array2string($bomail->icServer));
if (empty($bomail->icServer->acc_imap_host))
{
error_log(__METHOD__.__LINE__.' (user: '.$currentRecipient->account_lid.') notification for Profile:'.$activeProfile.' failed: NO IMAP HOST configured!');
continue; //fail silently
}
*/
try
{
$bomail->openConnection($activeProfile);
} catch (Exception $e) {
// TODO: This is ugly. Log a bit nicer!
$error = $e->getMessage();
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
}
//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]));
// 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").':'.$accountsToSearchArray[$activeProfile];
$values = array();
$values[] = array(); // content array starts at index 1
foreach($recent_messages as $id=>$recent_message) {
error_log(__METHOD__.__LINE__.' Found Message for Profile '.$activeProfile.':'.array2string($recent_message));
$values[] = array(
'mail_uid' => $recent_message['uid'],
'mail_folder' => $recent_message['folder_display_name'],
'mail_folder_base64' => $recent_message['folder_base64'],
'mail_subject' => $recent_message['subject'],
'mail_from' => !empty($recent_message['sender_name']) ? $recent_message['sender_name'] : $recent_message['sender_address'],
'mail_received' => $recent_message['date'],
);
// save notification status
$notified_mail_uidsCache[$activeProfile][$recent_message['folder']][] = mail_ui::generateRowID($activeProfile, $recent_message['folder'], $recent_message['uid'], $_prependApp=false);
//error_log(__METHOD__.__LINE__.' Found Messages for Profile'.$activeProfile.':'.array2string($recent_messages).'<->'.array2string($notified_mail_uidsCache[$activeProfile]));
if(count($recent_messages) > 0) {
// create notify message
$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) {
//error_log(__METHOD__.__LINE__.' Found Message for Profile '.$activeProfile.':'.array2string($recent_message));
$values[] = array(
'mail_uid' => $recent_message['uid'],
'mail_folder' => $recent_message['folder_display_name'],
'mail_folder_base64' => $recent_message['folder_base64'],
'mail_subject' => mail_bo::adaptSubjectForImport($recent_message['subject']),
'mail_from' => !empty($recent_message['sender_name']) ? $recent_message['sender_name'] : $recent_message['sender_address'],
'mail_received' => $recent_message['date'],
);
// save notification status
$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($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();
}
// 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);
} catch (Exception $e) {
// fail silently per server, if possible
error_log(__METHOD__.__LINE__.' Notification on new messages for Profile '.$activeProfile.' ('.$accountsToSearchArray[$activeProfile].') failed:'.$e->getMessage());
}
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;

View File

@ -763,10 +763,20 @@ class mail_ui
$out = array('id' => 0);
//$starttime = microtime(true);
foreach(emailadmin_account::search($only_current_user=true, $just_name=true) as $acc_id => $identity_name)
foreach(emailadmin_account::search($only_current_user=true, $just_name=false) as $acc_id => $accountObj)
{
if ($_profileID && $acc_id != $_profileID) continue;
if ($_profileID && $acc_id != $_profileID)
{
//error_log(__METHOD__.__LINE__.' Fetching accounts '." $acc_id != $_profileID ".'->'.$identity_name);
continue;
}
//error_log(__METHOD__.__LINE__.array2string($accountObj));
if (empty($accountObj->acc_imap_host))
{
// not to be used for IMAP Foldertree, as there is no Imap host
continue;
}
$identity_name = emailadmin_account::identity_name($accountObj);
$oA = array('id' => $acc_id,
'text' => str_replace(array('<','>'),array('[',']'),$identity_name),// as angle brackets are quoted, display in Javascript messages when used is ugly, so use square brackets instead
'tooltip' => '('.$acc_id.') '.htmlspecialchars_decode($identity_name),