From cf0574dbd766880c1681127e892f0aadcc53b8e1 Mon Sep 17 00:00:00 2001 From: Amani Nyumu Date: Wed, 19 Jun 2024 10:05:59 +0100 Subject: [PATCH] [ENH] add functionality to send later scheduled sending --- language/az.php | 1 + language/de.php | 1 + language/en.php | 2 +- language/es.php | 1 + language/et.php | 1 + language/fa.php | 2 +- language/fr.php | 1 + language/hu.php | 1 + language/id.php | 1 + language/it.php | 1 + language/ja.php | 1 + language/nl.php | 1 + language/pt-BR.php | 1 + language/ro.php | 1 + language/ru.php | 1 + language/zh-Hans.php | 1 + modules/core/functions.php | 99 ++++++++ modules/core/message_list_functions.php | 6 +- modules/core/site.css | 2 +- modules/core/site.js | 49 +++- modules/imap/functions.php | 69 ++++-- modules/imap/handler_modules.php | 7 +- modules/imap/hm-imap.php | 11 +- modules/imap/output_modules.php | 7 +- modules/imap/site.css | 4 +- modules/imap/site.js | 102 ++++----- modules/profiles/functions.php | 4 +- modules/profiles/hm-profiles.php | 10 + modules/smtp/hm-mime-message.php | 14 +- modules/smtp/modules.php | 288 +++++++++++++++++++++++- modules/smtp/setup.php | 29 ++- modules/smtp/site.js | 53 ++++- 32 files changed, 657 insertions(+), 115 deletions(-) diff --git a/language/az.php b/language/az.php index 1c4b5f56c5..31fd978183 100755 --- a/language/az.php +++ b/language/az.php @@ -621,6 +621,7 @@ 'You must provide a name for your script' => false, 'Empty script' => false, 'Please create a profile for saving sent messages option' => false, + 'You have %d scheduled messages that won\'t be executed if you quit' => false, 'Attachment storage unavailable, please contact your site administrator' => false, 'Your subject is empty!' => false, 'Your body is empty!' => false, diff --git a/language/de.php b/language/de.php index 86c1175517..11ba58e4fb 100755 --- a/language/de.php +++ b/language/de.php @@ -629,4 +629,5 @@ 'Junk' => false, 'Trash' => false, 'Pasted text has leading or trailing spaces' => false, + 'You have %d scheduled messages that won\'t be executed if you quit' => false, ); diff --git a/language/en.php b/language/en.php index 07ddd4642d..02732e1395 100755 --- a/language/en.php +++ b/language/en.php @@ -645,6 +645,6 @@ 'Are you sure you want to send this message?' => false, 'IMAP and JMAP Servers' => false, 'Junk' => false, - 'Trash' => false, 'Pasted text has leading or trailing spaces' => false, + 'You have %d scheduled messages that won\'t be executed if you quit' => false ); diff --git a/language/es.php b/language/es.php index d6d0eba2fa..2f3bc56601 100755 --- a/language/es.php +++ b/language/es.php @@ -629,4 +629,5 @@ 'Junk' => false, 'Trash' => false, 'Pasted text has leading or trailing spaces' => false, + 'You have %d scheduled messages that won\'t be executed if you quit' => false, ); diff --git a/language/et.php b/language/et.php index 692cd1845e..740ae62bd3 100755 --- a/language/et.php +++ b/language/et.php @@ -637,4 +637,5 @@ 'Junk' => false, 'Trash' => false, 'Pasted text has leading or trailing spaces' => false, + 'You have %d scheduled messages that won\'t be executed if you quit' => false, ); diff --git a/language/fa.php b/language/fa.php index 4b09e22d9e..d17798d5fd 100755 --- a/language/fa.php +++ b/language/fa.php @@ -679,6 +679,6 @@ 'Are you sure you want to send this message?' => false, 'IMAP and JMAP Servers' => false, 'Junk' => false, - 'Trash' => false, 'Pasted text has leading or trailing spaces' => false, + 'You have %d scheduled messages that won\'t be executed if you quit' => false, ); diff --git a/language/fr.php b/language/fr.php index 249f791af5..197a668094 100755 --- a/language/fr.php +++ b/language/fr.php @@ -628,4 +628,5 @@ 'Junk' => false, 'Trash' => false, 'Pasted text has leading or trailing spaces' => false, + 'You have %d scheduled messages that won\'t be executed if you quit' => false, ); diff --git a/language/hu.php b/language/hu.php index 535b619835..2cf2865394 100755 --- a/language/hu.php +++ b/language/hu.php @@ -629,4 +629,5 @@ 'Junk' => false, 'Trash' => false, 'Pasted text has leading or trailing spaces' => false, + 'You have %d scheduled messages that won\'t be executed if you quit' => false, ); diff --git a/language/id.php b/language/id.php index cd1871c227..731026fcef 100755 --- a/language/id.php +++ b/language/id.php @@ -636,4 +636,5 @@ 'Junk' => false, 'Trash' => false, 'Pasted text has leading or trailing spaces' => false, + 'You have %d scheduled messages that won\'t be executed if you quit' => false, ); diff --git a/language/it.php b/language/it.php index 8a23fa4f85..108d46a1c3 100755 --- a/language/it.php +++ b/language/it.php @@ -629,4 +629,5 @@ 'Junk' => false, 'Trash' => false, 'Pasted text has leading or trailing spaces' => false, + 'You have %d scheduled messages that won\'t be executed if you quit' => false, ); diff --git a/language/ja.php b/language/ja.php index 70f51af2ed..ddf4c907c3 100755 --- a/language/ja.php +++ b/language/ja.php @@ -629,4 +629,5 @@ 'Junk' => false, 'Trash' => false, 'Pasted text has leading or trailing spaces' => false, + 'You have %d scheduled messages that won\'t be executed if you quit' => false, ); diff --git a/language/nl.php b/language/nl.php index 81759970bf..7af9d9ec7c 100755 --- a/language/nl.php +++ b/language/nl.php @@ -629,4 +629,5 @@ 'Junk' => false, 'Trash' => false, 'Pasted text has leading or trailing spaces' => false, + 'You have %d scheduled messages that won\'t be executed if you quit' => false, ); diff --git a/language/pt-BR.php b/language/pt-BR.php index 30817ca379..6c00e742b0 100755 --- a/language/pt-BR.php +++ b/language/pt-BR.php @@ -628,4 +628,5 @@ 'Junk' => false, 'Trash' => false, 'Pasted text has leading or trailing spaces' => false, + 'You have %d scheduled messages that won\'t be executed if you quit' => false, ); diff --git a/language/ro.php b/language/ro.php index 7dc0e165a6..cd5326f551 100755 --- a/language/ro.php +++ b/language/ro.php @@ -628,4 +628,5 @@ 'Junk' => false, 'Trash' => false, 'Pasted text has leading or trailing spaces' => false, + 'You have %d scheduled messages that won\'t be executed if you quit' => false, ); diff --git a/language/ru.php b/language/ru.php index 1edbe98621..846d462495 100755 --- a/language/ru.php +++ b/language/ru.php @@ -630,4 +630,5 @@ 'Junk' => false, 'Trash' => false, 'Pasted text has leading or trailing spaces' => false, + 'You have %d scheduled messages that won\'t be executed if you quit' => false, ); diff --git a/language/zh-Hans.php b/language/zh-Hans.php index 66729faab6..3fe1513c58 100644 --- a/language/zh-Hans.php +++ b/language/zh-Hans.php @@ -650,4 +650,5 @@ 'Junk' => '垃圾', 'Trash' => '已删除', 'Pasted text has leading or trailing spaces' => false, + 'You have %d scheduled messages that won\'t be executed if you quit' => false, ); diff --git a/modules/core/functions.php b/modules/core/functions.php index 164aeaafc5..dfadeb22cf 100644 --- a/modules/core/functions.php +++ b/modules/core/functions.php @@ -607,3 +607,102 @@ function get_special_folders($mod, $id) { } return array(); } + +/** + * @subpackage core/functions + */ +if (!hm_exists('get_nexter_date')) { +function get_nexter_date($format, $only_label = false) { + if ($format == 'later_in_day') { + $date_string = 'today 18:00'; + $label = 'Later in the day'; + } elseif ($format == 'tomorrow') { + $date_string = '+1 day 08:00'; + $label = 'Tomorrow'; + } elseif ($format == 'next_weekend') { + $date_string = 'next Saturday 08:00'; + $label = 'Next weekend'; + } elseif ($format == 'next_week') { + $date_string = 'next week 08:00'; + $label = 'Next week'; + } elseif ($format == 'next_month') { + $date_string = 'next month 08:00'; + $label = 'Next month'; + } else { + $date_string = $format; + $label = 'Certain date'; + } + $time = strtotime($date_string); + if ($only_label) { + return [$label, date('D, H:i', $time)]; + } + return date('D, d M Y H:i', $time); +}} + +/** + * @subpackage imap/functions + */ +if (!hm_exists('nexter_formats')) { +function nexter_formats() { + $values = array( + 'tomorrow', + 'next_weekend', + 'next_week', + 'next_month' + ); + if (date('H') <= 16) { + array_push($values, 'later_in_day'); + } + return $values; +}} + +if (!hm_exists('schedule_dropdown')) { +function schedule_dropdown($output, $send_now = false) { + $values = nexter_formats(); + + $txt = ''; + if ($send_now) { + $txt .= ''; + } + + return $txt; +}} + +/** + * @subpackage imap/functions + */ +if (!hm_exists('parse_nexter_header')) { + function parse_nexter_header($header, $name) + { + $header = str_replace("$name: ", '', $header); + $result = []; + foreach (explode(';', $header) as $kv) + { + $kv = trim($kv); + $spacePos = strpos($kv, ' '); + if ($spacePos > 0) { + $result[rtrim(substr($kv, 0, $spacePos), ':')] = trim(substr($kv, $spacePos+1)); + } else { + $result[$kv] = true; + } + } + return $result; + }} diff --git a/modules/core/message_list_functions.php b/modules/core/message_list_functions.php index 11783809b8..ea21703a22 100644 --- a/modules/core/message_list_functions.php +++ b/modules/core/message_list_functions.php @@ -299,11 +299,11 @@ function subject_callback($vals, $style, $output_mod) { */ if (!hm_exists('date_callback')) { function date_callback($vals, $style, $output_mod) { - $snooze_class = isset($vals[2]) && $vals[2]? ' snoozed_date': ''; + $nexter_class = isset($vals[2]) && $vals[2]? ' nexter_date': ''; if ($style == 'news') { - return sprintf('
%s
', $snooze_class, $output_mod->html_safe($vals[0]), $output_mod->html_safe($vals[1])); + return sprintf('
%s
', $nexter_class, $output_mod->html_safe($vals[0]), $output_mod->html_safe($vals[1])); } - return sprintf('%s', $snooze_class, $output_mod->html_safe(date('r', $vals[1])), $output_mod->html_safe($vals[0]), $output_mod->html_safe($vals[1])); + return sprintf('%s', $nexter_class, $output_mod->html_safe(date('r', $vals[1])), $output_mod->html_safe($vals[0]), $output_mod->html_safe($vals[1])); }} /** diff --git a/modules/core/site.css b/modules/core/site.css index 262ac4acc5..df1874717b 100644 --- a/modules/core/site.css +++ b/modules/core/site.css @@ -1272,7 +1272,7 @@ div.unseen, .mobile .imap_sort { width: 100%; } -.snoozed_date { +.nexter_date { color: var(--bs-primary) !important; } diff --git a/modules/core/site.js b/modules/core/site.js index 8b2a7bafaf..b373f347e0 100644 --- a/modules/core/site.js +++ b/modules/core/site.js @@ -1944,10 +1944,23 @@ var imap_smtp_edit_action = function(event) { } }; + var hasLeadingOrTrailingSpaces = function(str) { return str !== str.trim(); }; +var sprintf = function(format, ...args) { + let i = 0; + return format.replace(/%([sd])/g, (match, type) => { + let arg = args[i++]; + switch (type) { + case 's': return String(arg); + case 'd': return Number(arg); + default: return match; + } + }); +} + /* create a default message list object */ var Hm_Message_List = new Message_List(); @@ -1980,7 +1993,7 @@ $(function() { /* fire up the job scheduler */ Hm_Timer.fire(); - + /* show any pending notices */ Hm_Utils.show_sys_messages(); @@ -2010,7 +2023,7 @@ $(function() { } if (hm_page_name() == 'servers') { $('.edit_server_connection').on('click', imap_smtp_edit_action); - } + } if (hm_mobile()) { swipe_event(document.body, function() { Hm_Folders.open_folder_list(); }, 'right'); swipe_event(document.body, function() { Hm_Folders.hide_folder_list(); }, 'left'); @@ -2368,7 +2381,7 @@ function handleSmtpImapCheckboxChange(checkbox) { if ($('#srv_setup_stepper_is_sender').prop('checked') && $('#srv_setup_stepper_is_receiver').prop('checked')) { $('#srv_setup_stepper_profile_bloc').show(); $('#srv_setup_stepper_profile_checkbox_bloc').show(); - + } else if(! $('#srv_setup_stepper_is_sender').prop('checked') || ! $('#srv_setup_stepper_is_receiver').prop('checked')) { $('#srv_setup_stepper_profile_bloc').hide(); $('#srv_setup_stepper_profile_checkbox_bloc').hide(); @@ -2420,7 +2433,7 @@ function display_config_step(stepNumber) { $(`#${item.key}-error`).text('Required'); isValid = false; } - + } else { $(`#${item.key}-error`).text(''); } @@ -2698,7 +2711,7 @@ const observeMessageTextMutationAndHandleExternalResources = (inline) => { if (mutation.addedNodes.length > 0) { mutation.addedNodes.forEach(function (node) { if (node.classList.contains('msg_text_inner')) { - handleExternalResources(inline); + handleExternalResources(inline); } }); } @@ -2708,3 +2721,29 @@ const observeMessageTextMutationAndHandleExternalResources = (inline) => { }); } }; + +var setup_nexter_date = function(callback) { + $(document).on('click', '.nexter_date_picker', function(e) { + document.querySelector('.nexter_input_date').showPicker(); + }); + $(document).on('click', '.nexter_date_helper', function(e) { + e.preventDefault(); + $('.nexter_input').val($(this).attr('data-value')).trigger('change'); + }); + $(document).on('input', '.nexter_input_date', function(e) { + var now = new Date(); + now.setMinutes(now.getMinutes() + 1); + $(this).attr('min', now.toJSON().slice(0, 16)); + if (new Date($(this).val()).getTime() <= now.getTime()) { + $('.nexter_date_picker').css('border', '1px solid red'); + } else { + $('.nexter_date_picker').css({'border': 'unset', 'border-top': '1px solid #ddd'}); + } + }); + $(document).on('change', '.nexter_input_date', function(e) { + if ($(this).val() && new Date().getTime() < new Date($(this).val()).getTime()) { + $('.nexter_input').val($(this).val()).trigger('change'); + } + }); + $(document).on('change', '.nexter_input', callback); +} diff --git a/modules/imap/functions.php b/modules/imap/functions.php index 34242d599d..7d5ed8f884 100644 --- a/modules/imap/functions.php +++ b/modules/imap/functions.php @@ -230,10 +230,14 @@ function format_imap_message_list($msg_list, $output_module, $parent_list=false, $nofrom = ' nofrom'; } $is_snoozed = !empty($msg['x_snoozed']) && hex2bin($msg['folder']) == 'Snoozed'; + $is_scheduled = !empty($msg['x_schedule']) && hex2bin($msg['folder']) == 'Scheduled'; if ($is_snoozed) { - $snooze_header = parse_snooze_header('X-Snoozed: '.$msg['x_snoozed']); + $snooze_header = parse_nexter_header('X-Snoozed: '.$msg['x_snoozed'], 'X-Snoozed'); $date = $snooze_header['until']; $timestamp = strtotime($date); + } elseif ($is_scheduled) { + $date = $msg['x_schedule']; + $timestamp = strtotime($date); } else { if ($list_sort == 'date') { $date_field = 'date'; @@ -308,7 +312,7 @@ function format_imap_message_list($msg_list, $output_module, $parent_list=false, array('safe_output_callback', 'source', $source, $icon), array('safe_output_callback', 'from'.$nofrom, $from, null, str_replace(array($from, '<', '>'), '', $msg['from'])), array('subject_callback', $subject, $url, $flags), - array('date_callback', $date, $timestamp, $is_snoozed), + array('date_callback', $date, $timestamp, $is_snoozed || $is_scheduled), array('icon_callback', $flags) ), $id, @@ -1279,7 +1283,7 @@ function snooze_message($imap, $msg_id, $folder, $snooze_tag) { preg_match("/^X-Snoozed:.*(\r?\n[ \t]+.*)*\r?\n?/im", $msg, $matches); if (count($matches)) { $msg = str_replace($matches[0], '', $msg); - $old_folder = parse_snooze_header($matches[0])['from']; + $old_folder = parse_nexter_header($matches[0], 'X-Snoozed')['from']; } if ($snooze_tag) { $from = $old_folder ?? $folder; @@ -1305,7 +1309,7 @@ function snooze_message($imap, $msg_id, $folder, $snooze_tag) { } } } else { - $snooze_headers = parse_snooze_header($matches[0]); + $snooze_headers = parse_nexter_header($matches[0], 'X-Snoozed'); $original_folder = $snooze_headers['from']; if ($imap->select_mailbox($original_folder) && $imap->append_start($original_folder, mb_strlen($msg))) { $imap->append_feed($msg."\r\n"); @@ -1394,21 +1398,21 @@ function snooze_formats() { */ if (!hm_exists('snooze_dropdown')) { function snooze_dropdown($output, $unsnooze = false) { - $values = snooze_formats(); + $values = nexter_formats(); $txt = ''; @@ -1499,3 +1503,42 @@ function connect_to_imap_server($address, $name, $port, $user, $pass, $tls, $ima } } } + +if (!hm_exists('save_sent_msg')) { +function save_sent_msg($handler, $imap_id, $imap, $imap_details, $msg, $msg_id, $show_errors = true) { + $specials = get_special_folders($handler, $imap_id); + if (array_key_exists('sent', $specials) && $specials['sent']) { + $sent_folder = $specials['sent']; + } + + if (!$sent_folder) { + $auto_sent = $imap->get_special_use_mailboxes('sent'); + if (!array_key_exists('sent', $auto_sent)) { + return; + } + $sent_folder = $auto_sent['sent']; + } + if (!$sent_folder) { + Hm_Debug::add(sprintf("Unable to save sent message, no sent folder for IMAP %s", $imap_details['server'])); + } + $uid = null; + if ($sent_folder) { + Hm_Debug::add(sprintf("Attempting to save sent message for IMAP server %s in folder %s", $imap_details['server'], $sent_folder)); + if ($imap->append_start($sent_folder, strlen($msg), true)) { + $imap->append_feed($msg."\r\n"); + if (!$imap->append_end() && $show_errors) { + Hm_Msgs::add('ERRAn error occurred saving the sent message'); + } + } + + $mailbox_page = $imap->get_mailbox_page($sent_folder, 'ARRIVAL', true, 'ALL', 0, 10); + foreach ($mailbox_page[1] as $mail) { + $msg_header = $imap->get_message_headers($mail['uid']); + if ($msg_header['Message-Id'] === $msg_id) { + $uid = $mail['uid']; + break; + } + } + } + return $uid; +}} diff --git a/modules/imap/handler_modules.php b/modules/imap/handler_modules.php index f71159bd82..b50ef2c51d 100644 --- a/modules/imap/handler_modules.php +++ b/modules/imap/handler_modules.php @@ -344,6 +344,9 @@ public function process() { break; } } + } + $uid = save_sent_msg($this, $imap_id, $imap, $imap_details, $msg, $mime->get_headers()['Message-Id']); + if ($uid) { if ($uid && $this->user_config->get('review_sent_email_setting', false)) { $this->out('redirect_url', '?page=message&uid='.$uid.'&list_path=imap_'.$imap_id.'_'.bin2hex($sent_folder)); } @@ -1016,7 +1019,7 @@ public function process() { $snooze_tag = null; if ($form['imap_snooze_until'] != 'unsnooze') { $at = date('D, d M Y H:i:s O'); - $until = get_snooze_date($form['imap_snooze_until']); + $until = get_nexter_date($form['imap_snooze_until']); $snooze_tag = "X-Snoozed: at $at; until $until"; } $ids = explode(',', $form['imap_snooze_ids']); @@ -1068,7 +1071,7 @@ public function process() { $msg_headers = $imap->get_message_headers($msg['uid']); if (isset($msg_headers['X-Snoozed'])) { try { - $snooze_headers = parse_snooze_header($msg_headers['X-Snoozed']); + $snooze_headers = parse_nexter_header($msg_headers['X-Snoozed'], 'X-Snoozed'); if (new DateTime($snooze_headers['until']) <= new DateTime()) { snooze_message($imap, $msg['uid'], $folder, null); } diff --git a/modules/imap/hm-imap.php b/modules/imap/hm-imap.php index 97261ddd7a..9de6d1e5e1 100644 --- a/modules/imap/hm-imap.php +++ b/modules/imap/hm-imap.php @@ -775,7 +775,7 @@ public function get_message_list($uids, $raw=false) { if ($this->is_supported( 'X-GM-EXT-1' )) { $command .= 'X-GM-MSGID X-GM-THRID X-GM-LABELS '; } - $command .= "BODY.PEEK[HEADER.FIELDS (SUBJECT X-AUTO-BCC FROM DATE CONTENT-TYPE X-PRIORITY TO LIST-ARCHIVE REFERENCES MESSAGE-ID X-SNOOZED)])\r\n"; + $command .= "BODY.PEEK[HEADER.FIELDS (SUBJECT X-AUTO-BCC FROM DATE CONTENT-TYPE X-PRIORITY TO LIST-ARCHIVE REFERENCES MESSAGE-ID X-SNOOZED X-SCHEDULE X-PROFILE-ID X-DELIVERY)])\r\n"; $cache_command = $command.(string)$raw; $cache = $this->check_cache($cache_command); if ($cache !== false) { @@ -785,8 +785,8 @@ public function get_message_list($uids, $raw=false) { $res = $this->get_response(false, true); $status = $this->check_response($res, true); $tags = array('X-GM-MSGID' => 'google_msg_id', 'X-GM-THRID' => 'google_thread_id', 'X-GM-LABELS' => 'google_labels', 'UID' => 'uid', 'FLAGS' => 'flags', 'RFC822.SIZE' => 'size', 'INTERNALDATE' => 'internal_date'); - $junk = array('X-AUTO-BCC', 'MESSAGE-ID', 'REFERENCES', 'X-SNOOZED', 'LIST-ARCHIVE', 'SUBJECT', 'FROM', 'CONTENT-TYPE', 'TO', '(', ')', ']', 'X-PRIORITY', 'DATE'); - $flds = array('x-auto-bcc' => 'x_auto_bcc', 'message-id' => 'message_id', 'references' => 'references', 'x-snoozed' => 'x_snoozed', 'list-archive' => 'list_archive', 'date' => 'date', 'from' => 'from', 'to' => 'to', 'subject' => 'subject', 'content-type' => 'content_type', 'x-priority' => 'x_priority'); + $junk = array('X-AUTO-BCC', 'MESSAGE-ID', 'REFERENCES', 'X-SNOOZED', 'X-SCHEDULE', 'X-PROFILE-ID', 'X-DELIVERY', 'LIST-ARCHIVE', 'SUBJECT', 'FROM', 'CONTENT-TYPE', 'TO', '(', ')', ']', 'X-PRIORITY', 'DATE'); + $flds = array('x-auto-bcc' => 'x_auto_bcc', 'message-id' => 'message_id', 'references' => 'references', 'x-snoozed' => 'x_snoozed', 'x-schedule' => 'x_schedule', 'x-profile-id' => 'x_profile_id', 'x-delivery' => 'x_delivery', 'list-archive' => 'list_archive', 'date' => 'date', 'from' => 'from', 'to' => 'to', 'subject' => 'subject', 'content-type' => 'content_type', 'x-priority' => 'x_priority'); $headers = array(); foreach ($res as $n => $vals) { if (isset($vals[0]) && $vals[0] == '*') { @@ -808,6 +808,9 @@ public function get_message_list($uids, $raw=false) { $google_labels = ''; $x_auto_bcc = ''; $x_snoozed = ''; + $x_schedule = ''; + $x_profile_id = ''; + $x_delivery = ''; $count = count($vals); for ($i=0;$i<$count;$i++) { if ($vals[$i] == 'BODY[HEADER.FIELDS') { @@ -857,7 +860,7 @@ public function get_message_list($uids, $raw=false) { 'timestamp' => time(), 'charset' => $cset, 'x-priority' => $x_priority, 'google_msg_id' => $google_msg_id, 'google_thread_id' => $google_thread_id, 'google_labels' => $google_labels, 'list_archive' => $list_archive, 'references' => $references, 'message_id' => $message_id, 'x_auto_bcc' => $x_auto_bcc, - 'x_snoozed' => $x_snoozed); + 'x_snoozed' => $x_snoozed, 'x_schedule' => $x_schedule, 'x_profile_id' => $x_profile_id, 'x_delivery' => $x_delivery); if ($raw) { $headers[$uid] = array_map('trim', $headers[$uid]); diff --git a/modules/imap/output_modules.php b/modules/imap/output_modules.php index 783fba40c4..15a75c46eb 100644 --- a/modules/imap/output_modules.php +++ b/modules/imap/output_modules.php @@ -188,9 +188,9 @@ protected function output() { $txt .= $this->html_safe($value).''; } elseif ($fld == 'x-snoozed') { - $snooze_header = parse_snooze_header($value); + $snooze_header = parse_nexter_header($value, 'X-Snoozed'); $txt .= ''; - $txt .= $this->trans('Snoozed').''.$this->trans('Until').' '.$this->html_safe($snooze_header['until']).' Unsnooze'; + $txt .= $this->trans('Snoozed').''.$this->trans('Until').' '.$this->html_safe($snooze_header['until']).' Unsnooze'; } elseif ($fld == 'date') { try { @@ -370,6 +370,9 @@ protected function output() { $txt .= ' | '.$this->trans('Copy').''; $txt .= ' | '.$this->trans('Move').''; $txt .= ' | '.$this->trans('Archive').''; + if (isset($headers['X-Schedule'])) { + $txt .= ' | ' . schedule_dropdown($this, true); + } $is_draft = isset($headers['Flags']) && mb_stristr($headers['Flags'], 'draft'); if ($this->get('sieve_filters_enabled') && !$is_draft) { diff --git a/modules/imap/site.css b/modules/imap/site.css index 36696f622d..8ee141e14e 100644 --- a/modules/imap/site.css +++ b/modules/imap/site.css @@ -126,7 +126,7 @@ .header_links { padding-top: 10px !important; } -.header_links #dropdownMenuSnooze { +.header_links #dropdownMenuNexterDate { padding: 0; border: unset; font-variant: inherit; @@ -134,7 +134,7 @@ font-size: inherit; vertical-align: baseline; } -.header_links #dropdownMenuSnooze:hover { +.header_links #dropdownMenuNexterDate:hover { background-color: inherit; color: inherit; } diff --git a/modules/imap/site.js b/modules/imap/site.js index dab2563f51..daaead2626 100644 --- a/modules/imap/site.js +++ b/modules/imap/site.js @@ -1084,61 +1084,6 @@ var imap_folder_status = function() { } }; -var imap_setup_snooze = function() { - $(document).on('click', '.snooze_date_picker', function(e) { - document.querySelector('.snooze_input_date').showPicker(); - }); - $(document).on('click', '.snooze_helper', function(e) { - e.preventDefault(); - $('.snooze_input').val($(this).attr('data-value')).trigger('change'); - }); - $(document).on('input', '.snooze_input_date', function(e) { - var now = new Date(); - now.setMinutes(now.getMinutes() + 1); - $(this).attr('min', now.toJSON().slice(0, 16)); - if (new Date($(this).val()).getTime() <= now.getTime()) { - $('.snooze_date_picker').css('border', '1px solid red'); - } else { - $('.snooze_date_picker').css({'border': 'unset', 'border-top': '1px solid #ddd'}); - } - }); - $(document).on('change', '.snooze_input_date', function(e) { - if ($(this).val() && new Date().getTime() < new Date($(this).val()).getTime()) { - $('.snooze_input').val($(this).val()).trigger('change'); - } - }); - $(document).on('change', '.snooze_input', function(e) { - $('.snooze_dropdown').hide(); - var ids = []; - if (hm_page_name() == 'message') { - var list_path = hm_list_path().split('_'); - ids.push(list_path[1]+'_'+hm_msg_uid()+'_'+list_path[2]); - } else { - $('input[type=checkbox]').each(function() { - if (this.checked && this.id.search('imap') != -1) { - var parts = this.id.split('_'); - ids.push(parts[1]+'_'+parts[2]+'_'+parts[3]); - } - }); - if (ids.length == 0) { - return; - }; - } - Hm_Ajax.request( - [{'name': 'hm_ajax_hook', 'value': 'ajax_imap_snooze'}, - {'name': 'imap_snooze_ids', 'value': ids}, - {'name': 'imap_snooze_until', 'value': $(this).val()}], - function(res) { - if (res.snoozed_messages > 0) { - Hm_Folders.reload_folders(true); - var path = hm_list_parent()? hm_list_parent(): hm_list_path(); - window.location.replace('?page=message_list&list_path='+path); - } - } - ); - }); -} - var imap_unsnooze_messages = function() { Hm_Ajax.request( [{'name': 'hm_ajax_hook', 'value': 'ajax_imap_unsnooze'}], @@ -1196,7 +1141,52 @@ $(function() { } if (hm_page_name() === 'message_list' || hm_page_name() === 'message') { - imap_setup_snooze(); + setup_nexter_date(function(e) { + $('.nexter_dropdown').hide(); + var ids = []; + if (hm_page_name() == 'message') { + var list_path = hm_list_path().split('_'); + ids.push(list_path[1]+'_'+hm_msg_uid()+'_'+list_path[2]); + } else { + $('input[type=checkbox]').each(function() { + if (this.checked && this.id.search('imap') != -1) { + var parts = this.id.split('_'); + ids.push(parts[1]+'_'+parts[2]+'_'+parts[3]); + } + }); + if (ids.length == 0) { + return; + }; + } + var reload_and_redirect = function() { + Hm_Folders.reload_folders(true); + var path = hm_list_parent()? hm_list_parent(): hm_list_path(); + window.location.replace('?page=message_list&list_path='+path); + } + if ($(this).parent().parent().is('.snooze_dropdown')) { + Hm_Ajax.request( + [{'name': 'hm_ajax_hook', 'value': 'ajax_imap_snooze'}, + {'name': 'imap_snooze_ids', 'value': ids}, + {'name': 'imap_snooze_until', 'value': $(this).val()}], + function(res) { + if (res.snoozed_messages > 0) { + reload_and_redirect(); + } + } + ); + } else { + Hm_Ajax.request( + [{'name': 'hm_ajax_hook', 'value': 'ajax_re_schedule_message_sending'}, + {'name': 'scheduled_msg_ids', 'value': ids}, + {'name': 'schedule_date', 'value': $(this).val()}], + function(res) { + if (res.scheduled_msg_count > 0) { + reload_and_redirect(); + } + } + ); + } + }); } if (hm_is_logged()) { diff --git a/modules/profiles/functions.php b/modules/profiles/functions.php index c63eb3a9a6..992971e423 100644 --- a/modules/profiles/functions.php +++ b/modules/profiles/functions.php @@ -3,7 +3,7 @@ if (!defined('DEBUG_MODE')) { die(); } if (!hm_exists('add_profile')) { - function add_profile($name, $signature, $reply_to, $is_default, $email, $server_mail, $smtp_server_id, $imap_server_id, $context) { + function add_profile($name, $signature, $reply_to, $is_default, $email, $imap_server_id, $smtp_server_id) { $profile = array( 'name' => $name, 'sig' => $signature, @@ -11,7 +11,7 @@ function add_profile($name, $signature, $reply_to, $is_default, $email, $server_ 'replyto' => $reply_to, 'default' => $is_default, 'address' => $email, - 'server' => $server_mail, + 'server' => $imap_server_id, 'user' => $email, 'type' => 'imap' ); diff --git a/modules/profiles/hm-profiles.php b/modules/profiles/hm-profiles.php index 16997e0582..966827baa1 100644 --- a/modules/profiles/hm-profiles.php +++ b/modules/profiles/hm-profiles.php @@ -109,4 +109,14 @@ public static function loadLegacy($hmod) { } } } + + public static function search($fld, $value) { + $res = array(); + foreach (self::getAll() as $profile) { + if (!empty($profile[$fld]) && $profile[$fld] == $value) { + $res[] = $profile; + } + } + return $res; + } } diff --git a/modules/smtp/hm-mime-message.php b/modules/smtp/hm-mime-message.php index cb5d307f73..2c00df2669 100644 --- a/modules/smtp/hm-mime-message.php +++ b/modules/smtp/hm-mime-message.php @@ -21,10 +21,15 @@ class Hm_MIME_Msg { private $final_msg = ''; /* build mime message data */ - function __construct($to, $subject, $body, $from, $html=false, $cc='', $bcc='', $in_reply_to_id='', $from_name='', $reply_to='') { + function __construct($to, $subject, $body, $from, $html=false, $cc='', $bcc='', $in_reply_to_id='', $from_name='', $reply_to='', $delivery_receipt='', $schedule='', $profile_id = '') { if ($cc) { $this->headers['Cc'] = $cc; } + if ($schedule) { + $this->headers['X-Schedule'] = $schedule; + $this->headers['X-Profile-ID'] = $profile_id; + } + if ($in_reply_to_id) { $this->headers['In-Reply-To'] = $in_reply_to_id; } @@ -41,6 +46,9 @@ function __construct($to, $subject, $body, $from, $html=false, $cc='', $bcc='', else { $this->headers['Reply-To'] = $from; } + if ($delivery_receipt) { + $this->headers['X-Delivery'] = $delivery_receipt; + } $this->headers['To'] = $to; $this->headers['Subject'] = html_entity_decode($subject, ENT_QUOTES); $this->headers['Date'] = date('r'); @@ -190,7 +198,7 @@ function prep_fld($val, $name) { return $this->encode_fld($val); } - function find_addresses($str) { + static function find_addresses($str) { $res = array(); foreach (process_address_fld($str) as $vals) { $res[] = $vals['email']; @@ -210,7 +218,7 @@ function get_recipient_addresses() { else { continue; } - $res = array_merge($res, $this->find_addresses($v)); + $res = array_merge($res, self::find_addresses($v)); } return $res; } diff --git a/modules/smtp/modules.php b/modules/smtp/modules.php index d2c5704b63..f5031ad0f0 100644 --- a/modules/smtp/modules.php +++ b/modules/smtp/modules.php @@ -114,6 +114,8 @@ public function process() { } } + + /** * @subpackage smtp/handler */ @@ -164,6 +166,7 @@ public function process() { } } + /** * @subpackage smtp/handler */ @@ -273,6 +276,8 @@ public function process() { $draft_id = array_key_exists('draft_id', $this->request->post) ? $this->request->post['draft_id'] : false; $draft_notice = array_key_exists('draft_notice', $this->request->post) ? $this->request->post['draft_notice'] : false; $uploaded_files = array_key_exists('uploaded_files', $this->request->post) ? $this->request->post['uploaded_files'] : false; + $delivery_receipt = array_key_exists('compose_delivery_receipt', $this->request->post) ? $this->request->post['compose_delivery_receipt'] : false; + $schedule = array_key_exists('schedule', $this->request->post) ? $this->request->post['schedule'] : ''; if (array_key_exists('delete_uploaded_files', $this->request->post) && $this->request->post['delete_uploaded_files']) { delete_uploaded_files($this->session, $draft_id); @@ -287,16 +292,18 @@ public function process() { } $new_draft_id = save_imap_draft(array('draft_smtp' => $smtp, 'draft_to' => $to, 'draft_body' => $body, 'draft_subject' => $subject, 'draft_cc' => $cc, 'draft_bcc' => $bcc, - 'draft_in_reply_to' => $inreplyto), $draft_id, $this->session, + 'draft_in_reply_to' => $inreplyto, 'delivery_receipt' => $delivery_receipt, 'schedule' => $schedule), $draft_id, $this->session, $this, $this->cache, $uploaded_files); if ($new_draft_id >= 0) { if ($draft_notice) { - Hm_Msgs::add('Draft saved'); + $msg = $schedule ? 'Message scheduled to be sent later' : 'Draft saved'; + Hm_Msgs::add($msg); } $this->out('draft_id', $new_draft_id); } elseif ($draft_notice) { - Hm_Msgs::add('ERRUnable to save draft'); + $msg = $schedule ? 'Something went wrong when scheduling draft' : 'Unable to save draft'; + Hm_Msgs::add('ERR' . $msg); } return; } @@ -661,6 +668,7 @@ public function process() { 'draft_subject' => $form['compose_subject'], 'draft_smtp' => $smtp_id ); + $delivery_receipt = !empty($this->request->post['compose_delivery_receipt']); $from_params = ''; $recipients_params = ''; @@ -680,7 +688,7 @@ public function process() { /* msg details */ list($body, $cc, $bcc, $in_reply_to, $draft) = get_outbound_msg_detail($this->request->post, $draft, $body_type); - if (!empty($this->request->post['compose_delivery_receipt'])) { + if ($delivery_receipt) { $from_params = 'RET=HDRS'; $recipients_params = 'NOTIFY=SUCCESS,FAILURE'; } @@ -712,7 +720,7 @@ public function process() { } /* build message */ - $mime = new Hm_MIME_Msg($to, $subject, $body, $from, $body_type, $cc, $bcc, $in_reply_to, $from_name, $reply_to); + $mime = new Hm_MIME_Msg($to, $subject, $body, $from, $body_type, $cc, $bcc, $in_reply_to, $from_name, $reply_to, $delivery_receipt); /* add attachments */ $mime->add_attachments($uploaded_files); @@ -853,6 +861,132 @@ public function process() { } } +/** + * Send scheduled messages + * @subpackage smtp/handler + */ +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++; + } + } + } + } + } + $this->out('scheduled_msg_count', $scheduled_msg_count); + } +} + +/** + * Changes the schedule of the message + * @subpackage smtp/handler + */ +class Hm_Handler_re_schedule_message_sending extends Hm_Handler_Module { + public function process() { + if (!($this->module_is_supported('imap') || $this->module_is_supported('profiles'))) { + return; + } + list($success, $form) = $this->process_form(array('schedule_date', 'scheduled_msg_ids')); + if (!$success) { + return; + } + $scheduled_msg_count = 0; + if ($form['schedule_date'] != 'now') { + $new_schedule_date = get_nexter_date($form['schedule_date']); + } + $ids = explode(',', $form['scheduled_msg_ids']); + foreach ($ids as $msg_part) { + list($imap_server_id, $msg_id, $folder) = explode('_', $msg_part); + $cache = Hm_IMAP_List::get_cache($this->cache, $imap_server_id); + $imap = Hm_IMAP_List::connect($imap_server_id, $cache); + if (imap_authed($imap)) { + $folder = hex2bin($folder); + if (reschedule_message_sending($this, $imap, $msg_id, $folder, $new_schedule_date, $imap_server_id)) { + $scheduled_msg_count++; + } + } + } + $this->out('scheduled_msg_count', $scheduled_msg_count); + if ($scheduled_msg_count == count($ids)) { + $msg = 'Operation successful'; + } elseif ($scheduled_msg_count > 0) { + $msg = 'Some messages have been scheduled for sending'; + } else { + $msg = 'ERRFailed to schedule sending for messages'; + } + Hm_Msgs::add($msg); + $this->save_hm_msgs(); + } +} + /** * @subpackage keyboard_shortcuts/output */ @@ -1186,11 +1320,15 @@ protected function output() { } } } - $res .= ''. smtp_server_dropdown($this->module_output(), $this, $recip, $selected_id). - ''; - + '
+ + '. + schedule_dropdown($this). + '
'; if ($this->get('list_path') && ($reply_type == 'reply' || $reply_type == 'reply_all')) { $res .= ''; } @@ -1485,6 +1623,20 @@ protected function output() { } } +/** + * Add scheduled send to the message list controls + * @subpackage imap/output + */ +class Hm_Output_scheduled_send_msg_control extends Hm_Output_Module { + protected function output() { + $parts = explode('_', $this->get('list_path')); + if ($parts[0] == 'imap' && hex2bin($parts[2]) == 'Scheduled') { + $res = schedule_dropdown($this, true); + $this->concat('msg_controls_extra', $res); + } + } +} + /** * @subpackage smtp/functions */ @@ -1847,13 +1999,23 @@ function save_imap_draft($atts, $id, $session, $mod, $mod_cache, $uploaded_files $specials = get_special_folders($mod, $imap_profile['id']); - if (!array_key_exists('draft', $specials) || !$specials['draft']) { + if ((!array_key_exists('draft', $specials) || !$specials['draft']) && !array_key_exists('schedule', $atts)) { Hm_Msgs::add('ERRThere is no draft directory configured for this account.'); return -1; } $cache = Hm_IMAP_List::get_cache($mod_cache, $imap_profile['id']); $imap = Hm_IMAP_List::connect($imap_profile['id'], $cache); - $draft_folder = $imap->select_mailbox($specials['draft']); + + if (!empty($atts['schedule'])) { + $folder ='Scheduled'; + if (!count($imap->get_mailbox_status($folder))) { + $imap->create_mailbox($folder); + } + $atts['schedule'] = get_nexter_date($atts['schedule']); + } else { + $folder = $specials['draft']; + } + $imap->select_mailbox($folder); $mime = new Hm_MIME_Msg( $atts['draft_to'], @@ -1865,7 +2027,10 @@ function save_imap_draft($atts, $id, $session, $mod, $mod_cache, $uploaded_files $atts['draft_bcc'], '', $name, - $atts['draft_in_reply_to'] + $atts['draft_in_reply_to'], + $atts['delivery_receipt'], + $atts['schedule'], + $profile['id'] ); $mime->add_attachments($uploaded_files); @@ -1883,7 +2048,7 @@ function save_imap_draft($atts, $id, $session, $mod, $mod_cache, $uploaded_files } } - $mailbox_page = $imap->get_mailbox_page($specials['draft'], 'ARRIVAL', true, 'DRAFT', 0, 10); + $mailbox_page = $imap->get_mailbox_page($folder, 'ARRIVAL', true, 'DRAFT', 0, 10); // Remove old version from the mailbox if ($id) { @@ -2172,3 +2337,102 @@ function recip_count_check($headers, $omod) { Hm_Msgs::add('ERRMessage contains more than the maximum number of recipients, proceed with caution'); } }} + +/** + * @subpackage smtp/functions + */ +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; +}} + +if (!hm_exists('reschedule_message_sending')) { +function reschedule_message_sending($handler, $imap, $msg_id, $folder, $new_date, $server_id) { + if (!$imap->select_mailbox($folder)) { + return; + } + $msg = $imap->get_message_content($msg_id, 0); + if ($new_date == 'now') { + return send_scheduled_message($handler, $imap, $msg, $server_id); + } + preg_match("/^X-Schedule:.*(\r?\n[ \t]+.*)*\r?\n?/im", $msg, $matches); + if (count($matches)) { + $new_date = get_nexter_date($new_date); + $msg = str_replace($matches[0], "X-Schedule: {$new_date}\n", $msg); + } else { + return; + } + $msg = str_replace("\r\n", "\n", $msg); + $msg = str_replace("\n", "\r\n", $msg); + $msg = rtrim($msg)."\r\n"; + + $schedule_folder = 'Scheduled'; + if (!count($imap->get_mailbox_status($schedule_folder))) { + return; + } + $res = false; + if ($imap->select_mailbox($schedule_folder) && $imap->append_start($schedule_folder, strlen($msg))) { + $imap->append_feed($msg."\r\n"); + if ($imap->append_end()) { + if ($imap->select_mailbox($folder) && $imap->message_action('DELETE', array($msg_id))) { + $imap->message_action('EXPUNGE', array($msg_id)); + $res = true; + } + } + } + return $res; +}} diff --git a/modules/smtp/setup.php b/modules/smtp/setup.php index 8b14f4b91b..e53d690a8f 100644 --- a/modules/smtp/setup.php +++ b/modules/smtp/setup.php @@ -19,6 +19,7 @@ add_output('compose', 'compose_form_attach', true, 'smtp', 'compose_form_end', 'after'); add_handler('compose', 'load_smtp_is_imap_forward', true, 'smtp', 'load_user_data', 'after'); + add_handler('functional_api', 'default_smtp_server', true, 'smtp'); add_handler('profiles', 'load_smtp_servers_from_config', true, 'smtp', 'load_user_data', 'after'); @@ -100,6 +101,21 @@ add_handler('ajax_clear_attachment_chunks', 'load_user_data', true, 'core'); add_handler('ajax_clear_attachment_chunks', 'clear_attachment_chunks', true); +/* send delayed emails */ +setup_base_ajax_page('ajax_send_scheduled_messages', 'core'); +add_handler('ajax_send_scheduled_messages', 'load_imap_servers_from_config', true, 'imap', 'load_user_data', 'after'); +add_handler('ajax_send_scheduled_messages', 'load_smtp_servers_from_config', true, 'smtp', 'load_user_data', 'after'); +add_handler('ajax_send_scheduled_messages', 'compose_profile_data', true, 'profiles'); +add_handler('ajax_send_scheduled_messages', 'send_scheduled_messages', true, 'smtp'); + +setup_base_ajax_page('ajax_re_schedule_message_sending', 'core'); +add_handler('ajax_re_schedule_message_sending', 'load_imap_servers_from_config', true, 'imap', 'load_user_data', 'after'); +add_handler('ajax_re_schedule_message_sending', 'load_smtp_servers_from_config', true, 'smtp', 'load_user_data', 'after'); +add_handler('ajax_re_schedule_message_sending', 'compose_profile_data', true, 'profiles'); +add_handler('ajax_re_schedule_message_sending', 're_schedule_message_sending', true, 'smtp'); + +add_output('message_list', 'scheduled_send_msg_control', true, 'smtp', 'imap_custom_controls', 'after'); + return array( 'allowed_pages' => array( 'ajax_clear_attachment_chunks', @@ -109,7 +125,9 @@ 'ajax_profiles_status', 'ajax_attachment_reminder_check', 'ajax_get_test_chunk', - 'ajax_upload_chunk' + 'ajax_upload_chunk', + 'ajax_send_scheduled_messages', + 'ajax_re_schedule_message_sending' ), 'allowed_get' => array( 'imap_draft' => FILTER_VALIDATE_INT, @@ -140,9 +158,13 @@ 'msg_sent_and_archived' => array(FILTER_VALIDATE_BOOLEAN, false), 'sent_msg_id' => array(FILTER_VALIDATE_BOOLEAN, false), 'enable_attachment_reminder' => array(FILTER_VALIDATE_BOOLEAN, false), + 'scheduled_msg_count' => array(FILTER_VALIDATE_INT, false), ), 'allowed_post' => array( 'post_archive' => FILTER_VALIDATE_INT, + 'send_tomorrow_morning' => FILTER_DEFAULT, + 'send_today_afternoon' => FILTER_DEFAULT, + 'schedule_sending' => FILTER_DEFAULT, 'attachment_id' => FILTER_DEFAULT, 'smtp_compose_type' => FILTER_VALIDATE_INT, 'new_smtp_name' => FILTER_DEFAULT, @@ -182,6 +204,9 @@ 'uploaded_files' => FILTER_DEFAULT, 'send_uploaded_files' => FILTER_DEFAULT, 'next_email_post' => FILTER_DEFAULT, - 'enable_attachment_reminder' => FILTER_VALIDATE_INT + 'enable_attachment_reminder' => FILTER_VALIDATE_INT, + 'schedule' => FILTER_DEFAULT, + 'schedule_date' => FILTER_DEFAULT, + 'scheduled_msg_ids' => FILTER_DEFAULT, ) ); diff --git a/modules/smtp/site.js b/modules/smtp/site.js index 31981144c7..db95397670 100644 --- a/modules/smtp/site.js +++ b/modules/smtp/site.js @@ -113,7 +113,7 @@ var send_archive = function() { document.getElementsByClassName("smtp_send_placeholder")[0].click(); } -var save_compose_state = function(no_files, notice) { +var save_compose_state = function(no_files, notice, schedule, callback) { var no_icon = true; if (notice) { no_icon = false; @@ -126,6 +126,7 @@ var save_compose_state = function(no_files, notice) { var cc = $('.compose_cc').val(); var bcc = $('.compose_bcc').val(); var inreplyto = $('.compose_in_reply_to').val(); + var delivery_receipt = $('#compose_delivery_receipt').prop('checked'); var draft_id = $('.compose_draft_id').val(); if (globals.draft_state == body+subject+to+smtp+cc+bcc+uploaded_files) { @@ -138,7 +139,7 @@ var save_compose_state = function(no_files, notice) { } $('.smtp_send_placeholder').prop('disabled', true); - $('.smtp_send_placeholder').addClass('disabled_input'); + $('.smtp_send_placeholder, .smtp_schedule_send').addClass('disabled_input'); Hm_Ajax.request( [{'name': 'hm_ajax_hook', 'value': 'ajax_smtp_save_draft'}, {'name': 'draft_body', 'value': body}, @@ -151,16 +152,21 @@ var save_compose_state = function(no_files, notice) { {'name': 'draft_in_reply_to', 'value': inreplyto}, {'name': 'delete_uploaded_files', 'value': no_files}, {'name': 'draft_to', 'value': to}, + {'name': 'schedule', 'value': schedule}, + {'name': 'compose_delivery_receipt', 'value': delivery_receipt}, {'name': 'uploaded_files', 'value': uploaded_files}], function(res) { $('.smtp_send_placeholder').prop('disabled', false); - $('.smtp_send_placeholder').removeClass('disabled_input'); + $('.smtp_send_placeholder, .smtp_schedule_send').removeClass('disabled_input'); if (res.draft_id) { $('.compose_draft_id').val(res.draft_id); } if (res.draft_subject) { $('.draft_list .draft_'+draft_id+' a').text(res.draft_subject); } + if (callback) { + callback(res); + } }, [], no_icon @@ -188,7 +194,7 @@ if (hm_page_name() === 'servers') { } } -var reset_smtp_form = function() { +var reset_smtp_form = function(save = true) { $('.compose_body').val(''); $('.compose_subject').val(''); $('.compose_to').val(''); @@ -196,7 +202,10 @@ var reset_smtp_form = function() { $('.compose_bcc').val(''); $('.ke-content', $('iframe').contents()).html(''); $('.uploaded_files').html(''); - save_compose_state(true); + $('#compose_delivery_receipt').prop('checked', false); + if (save) { + save_compose_state(true); + } }; var replace_cursor_positon = function (txtElement) { @@ -419,6 +428,16 @@ var force_send_message = function() { } } +var send_scheduled_messages = function() { + Hm_Ajax.request( + [{'name': 'hm_ajax_hook', 'value': 'ajax_send_scheduled_messages'}], + function(res) { + scheduled_msg_count = res.scheduled_msg_count; + }, + ); +} +var scheduled_msg_count = 0; + $(function () { if (!hm_is_logged()) { return; @@ -437,6 +456,9 @@ $(function () { } if (hm_page_name() === 'compose') { init_resumable_upload() + setup_nexter_date(function() { + $('.smtp_send_placeholder').trigger('click'); + }); var interval = Hm_Utils.get_from_global('compose_save_interval', 30); Hm_Timer.add_job(function() { save_compose_state(); }, interval, true); @@ -516,7 +538,17 @@ $(function () { function handleSendAnyway() { if (handleMissingAttachment()) { - document.getElementsByClassName("smtp_send")[0].click(); + if ($('.nexter_input').val()) { + save_compose_state(false, true, $('.nexter_input').val(), function(res) { + if (!res.router_user_msgs[0].startsWith('ERR')) { + reset_smtp_form(false); + Hm_Folders.reload_folders(true); + Hm_Utils.redirect(); + } + }); + } else { + document.getElementsByClassName("smtp_send")[0].click(); + } } else { e.preventDefault(); } @@ -644,4 +676,13 @@ $(function () { } } + send_scheduled_messages(); + setInterval(send_scheduled_messages, 60000); + + window.onbeforeunload = (e) => { + if (scheduled_msg_count > 0 && e.currentTarget.location.hostname !== document.location.hostname) { + return sprintf(hm_trans("You have %d scheduled messages that won\'t be executed if you quit"), scheduled_msg_count); + } + return; + }; });