diff --git a/modules/smtp/modules.php b/modules/smtp/modules.php index 78281b5c5..e4e98f832 100644 --- a/modules/smtp/modules.php +++ b/modules/smtp/modules.php @@ -960,80 +960,44 @@ class Hm_Handler_send_scheduled_messages extends Hm_Handler_Module { * Send delayed messages * This should use cron */ - public function process() { - if (!($this->module_is_supported('imap') || $this->module_is_supported('profiles'))) { - return; - } - $servers = Hm_IMAP_List::dump(); - $scheduled_msg_count = 0; - foreach (array_keys($servers) as $server_id) { - $cache = Hm_IMAP_List::get_cache($this->cache, $server_id); - $imap = Hm_IMAP_List::connect($server_id, $cache); - if (imap_authed($imap)) { - $folder = 'Scheduled'; - $ret = $imap->get_mailbox_page($folder, 'DATE', false, 'ALL'); - foreach ($ret[1] as $msg) { - $msg_headers = $imap->get_message_headers($msg['uid']); - try { - if (!empty($msg_headers['X-Schedule'])) { - $scheduled_msg_count++; - } else { - continue; - } - if (new DateTime($msg_headers['X-Schedule']) <= new DateTime()) { - $profile = Hm_Profiles::get($msg_headers['X-Profile-ID']); - if (!$profile) { - $profiles = Hm_Profiles::search('server', $imap_details['server']); - - if (!$profiles) { - Hm_Debug::add(sprintf('ERRCannot find profiles corresponding with IMAP server: %s', $imap_details['server'])); - continue; - } - $profile = $profiles[0]; - } - - $smtp = Hm_SMTP_List::connect($profile['smtp_id'], false); - - if (smtp_authed($smtp)) { - if (isset($msg_headers['X-Delivery'])) { - $from_params = 'RET=HDRS'; - $recipients_params = 'NOTIFY=SUCCESS,FAILURE'; - } else { - $from_params = ''; - $recipients_params = ''; - } - $recipients = []; - foreach (['To', 'Cc', 'Bcc'] as $fld) { - if (array_key_exists($fld, $msg_headers)) { - $recipients = array_merge($recipients, Hm_MIME_Msg::find_addresses($msg_headers[$fld])); - } - } - $msg_content = $imap->get_message_content($msg['uid'], 0); - $from = process_address_fld($msg_headers['From']); - - $err_msg = $smtp->send_message($from[0]['email'], $recipients, $msg_content, $from_params, $recipients_params); - - if (!$err_msg) { - if ($imap->message_action('DELETE', [$msg['uid']])) { - $imap->message_action('EXPUNGE', [$msg['uid']]); - } - save_sent_msg($this, $server_id, $imap, $imap_details, $msg_content, $msg['uid'], false); - $scheduled_msg_count--; - } - } - } - } catch (Exception $e) { - Hm_Debug::add(sprintf('ERRCannot send message: %s', $msg_headers['subject'])); - if (send_scheduled_message($this, $imap, $msg, $server_id)) { - $scheduled_msg_count++; - } + public function process() { + if (!($this->module_is_supported('imap') || $this->module_is_supported('profiles'))) { + return; + } + + $servers = Hm_IMAP_List::dump(); + $scheduled_msg_count = 0; + + foreach (array_keys($servers) as $server_id) { + $cache = Hm_IMAP_List::get_cache($this->cache, $server_id); + $imap = Hm_IMAP_List::connect($server_id, $cache); + + if (imap_authed($imap)) { + $folder = 'Scheduled'; + $ret = $imap->get_mailbox_page($folder, 'DATE', false, 'ALL'); + + foreach ($ret[1] as $msg) { + $msg_headers = $imap->get_message_headers($msg['uid']); + + try { + if (!empty($msg_headers['X-Schedule'])) { + $scheduled_msg_count++; + } else { + continue; } + + if (send_message_if_scheduled($this, $imap, $msg, $server_id)) { + $scheduled_msg_count--; + } + } catch (Exception $e) { + Hm_Debug::add(sprintf('ERRCannot send message: %s', $msg_headers['subject'])); } } } - $this->out('scheduled_msg_count', $scheduled_msg_count); } -} + + $this->out('scheduled_msg_count', $scheduled_msg_count); +}} /** * Changes the schedule of the message @@ -2465,61 +2429,7 @@ function recip_count_check($headers, $omod) { */ if (!hm_exists('send_scheduled_message')) { function send_scheduled_message($handler, $imap, $msg, $server_id) { - $msg_headers = $imap->get_message_headers($msg['uid']); - $imap_details = Hm_IMAP_List::dump($server_id); - if (empty($imap_details)) { - return; - } - try { - if (empty($msg_headers['X-Schedule'])) { - return; - } - if (new DateTime($msg_headers['X-Schedule']) <= new DateTime()) { - $profile = Hm_Profiles::get($msg_headers['X-Profile-ID']); - if (!$profile) { - $profiles = Hm_Profiles::search('server', $imap_details['server']); - - if (!$profiles) { - Hm_Debug::add(sprintf('ERRCannot find profiles corresponding with IMAP server: %s', $imap_details['server'])); - return; - } - $profile = $profiles[0]; - } - - $smtp = Hm_SMTP_List::connect($profile['smtp_id'], false); - - if (smtp_authed($smtp)) { - if (isset($msg_headers['X-Delivery'])) { - $from_params = 'RET=HDRS'; - $recipients_params = 'NOTIFY=SUCCESS,FAILURE'; - } else { - $from_params = ''; - $recipients_params = ''; - } - $recipients = []; - foreach (['To', 'Cc', 'Bcc'] as $fld) { - if (array_key_exists($fld, $msg_headers)) { - $recipients = array_merge($recipients, Hm_MIME_Msg::find_addresses($msg_headers[$fld])); - } - } - $msg_content = $imap->get_message_content($msg['uid'], 0); - $from = process_address_fld($msg_headers['From']); - - $err_msg = $smtp->send_message($from[0]['email'], $recipients, $msg_content, $from_params, $recipients_params); - - if (!$err_msg) { - if ($imap->message_action('DELETE', [$msg['uid']])) { - $imap->message_action('EXPUNGE', [$msg['uid']]); - } - save_sent_msg($handler, $server_id, $imap, $imap_details, $msg_content, $msg['uid'], false); - return true; - } - } - } - } catch (Exception $e) { - Hm_Debug::add(sprintf('ERRCannot send message: %s', $msg_headers['subject'])); - } - return; + return send_message_if_scheduled($handler, $imap, $msg, $server_id); }} if (!hm_exists('reschedule_message_sending')) { @@ -2556,5 +2466,72 @@ function reschedule_message_sending($handler, $imap, $msg_id, $folder, $new_date } } } - return $res; + return $res; }} + +/** + * @subpackage smtp/functions + */ +if (!hm_exists('send_message_if_scheduled')) { + function send_message_if_scheduled($handler, $imap, $msg, $server_id) { + $msg_headers = $imap->get_message_headers($msg['uid']); + $imap_details = Hm_IMAP_List::dump($server_id); + if (empty($imap_details)) { + return false; + } + + try { + if (empty($msg_headers['X-Schedule'])) { + return false; + } + + if (new DateTime($msg_headers['X-Schedule']) <= new DateTime()) { + $profile = Hm_Profiles::get($msg_headers['X-Profile-ID']); + if (!$profile) { + $profiles = Hm_Profiles::search('server', $imap_details['server']); + + if (!$profiles) { + Hm_Debug::add(sprintf('ERRCannot find profiles corresponding with IMAP server: %s', $imap_details['server'])); + return false; + } + $profile = $profiles[0]; + } + + $smtp = Hm_SMTP_List::connect($profile['smtp_id'], false); + + if (smtp_authed($smtp)) { + if (isset($msg_headers['X-Delivery'])) { + $from_params = 'RET=HDRS'; + $recipients_params = 'NOTIFY=SUCCESS,FAILURE'; + } else { + $from_params = ''; + $recipients_params = ''; + } + + $recipients = []; + foreach (['To', 'Cc', 'Bcc'] as $fld) { + if (array_key_exists($fld, $msg_headers)) { + $recipients = array_merge($recipients, Hm_MIME_Msg::find_addresses($msg_headers[$fld])); + } + } + + $msg_content = $imap->get_message_content($msg['uid'], 0); + $from = process_address_fld($msg_headers['From']); + + $err_msg = $smtp->send_message($from[0]['email'], $recipients, $msg_content, $from_params, $recipients_params); + + if (!$err_msg) { + if ($imap->message_action('DELETE', [$msg['uid']])) { + $imap->message_action('EXPUNGE', [$msg['uid']]); + } + save_sent_msg($handler, $server_id, $imap, $imap_details, $msg_content, $msg['uid'], false); + return true; + } + } + } + } catch (Exception $e) { + Hm_Debug::add(sprintf('ERRCannot send message: %s', $msg_headers['subject'])); + } + return false; + } +}