Skip to content

Commit

Permalink
[ENH]add Cypht-delay-send-later-scheduled-sending
Browse files Browse the repository at this point in the history
  • Loading branch information
amaninyumu1 committed Oct 30, 2024
1 parent b2f879d commit 4698358
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 119 deletions.
8 changes: 1 addition & 7 deletions modules/core/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -2024,13 +2024,7 @@ $(function() {
if (hm_mailto()) {
try { navigator.registerProtocolHandler("mailto", "?page=compose&compose_to=%s", "Cypht"); } catch(e) {}
}

if (hm_page_name() == 'home') {
$('.pw_update').on("click", function() { update_password($(this).data('id')); });
}
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');
Expand Down
192 changes: 80 additions & 112 deletions modules/smtp/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_scheduled_message($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
Expand Down Expand Up @@ -2466,60 +2430,64 @@ 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;
$imap_details = Hm_IMAP_List::dump($server_id);
if (empty($imap_details)) {
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;
}
$profile = $profiles[0];
try {
if (empty($msg_headers['X-Schedule'])) {
return false;
}

$smtp = Hm_SMTP_List::connect($profile['smtp_id'], 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 (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]));
if (!$profiles) {
Hm_Debug::add(sprintf('ERRCannot find profiles corresponding with IMAP server: %s', $imap_details['server']));
return false;
}
$profile = $profiles[0];
}
$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);
$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']);

if (!$err_msg) {
if ($imap->message_action('DELETE', [$msg['uid']])) {
$imap->message_action('EXPUNGE', [$msg['uid']]);
$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;
}
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']));
}
} catch (Exception $e) {
Hm_Debug::add(sprintf('ERRCannot send message: %s', $msg_headers['subject']));
}
return;
return false;
}}

if (!hm_exists('reschedule_message_sending')) {
Expand Down Expand Up @@ -2556,5 +2524,5 @@ function reschedule_message_sending($handler, $imap, $msg_id, $folder, $new_date
}
}
}
return $res;
}}
return $res;
}}

0 comments on commit 4698358

Please sign in to comment.