Skip to content

Commit 6a1795f

Browse files
committed
feat(smtp): implement send_smtp_message function and refactor email sending logic to avoid repeated code
1 parent 73e9720 commit 6a1795f

File tree

2 files changed

+44
-52
lines changed

2 files changed

+44
-52
lines changed

modules/smtp/functions.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,30 @@ function reschedule_message_sending($handler, $mailbox, $msg_id, $folder, $new_d
149149
}
150150
return $res;
151151
}}
152+
153+
if (!hm_exists('send_smtp_message')) {
154+
function send_smtp_message($handler_mod, $smtp_id, $from_address, $recipients, $mime_message, $delivery_receipt = false) {
155+
$smtp_details = Hm_SMTP_List::dump($smtp_id, true);
156+
if (!$smtp_details) {
157+
Hm_Msgs::add('Could not load selected SMTP server details.', 'danger');
158+
return 'Could not load selected SMTP server details.';
159+
}
160+
161+
// Refresh OAuth2 token if needed
162+
smtp_refresh_oauth2_token_on_send($smtp_details, $handler_mod, $smtp_id);
163+
164+
$smtp_mailbox = Hm_SMTP_List::connect($smtp_id, false);
165+
if (!$smtp_mailbox || !$smtp_mailbox->authed()) {
166+
Hm_Msgs::add("Failed to authenticate to the SMTP server.", "danger");
167+
return "Failed to authenticate to the SMTP server.";
168+
}
169+
170+
$err_msg = $smtp_mailbox->send_message($from_address, $recipients, $mime_message, $delivery_receipt);
171+
172+
if (!$err_msg) {
173+
return false; // Indicates success
174+
} else {
175+
Hm_Msgs::add('Failed to send email: ' . $err_msg, 'danger');
176+
return $err_msg;
177+
}
178+
}}

modules/smtp/modules.php

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -690,32 +690,13 @@ public function process() {
690690
/* msg details */
691691
list($body, $cc, $bcc, $in_reply_to, $draft) = get_outbound_msg_detail($this->request->post, $draft, $body_type);
692692

693-
/* smtp server details */
694-
$smtp_details = Hm_SMTP_List::dump($smtp_id, true);
695-
if (!$smtp_details) {
696-
Hm_Msgs::add('Could not use the selected SMTP server', 'warning');
697-
repopulate_compose_form($draft, $this);
698-
return;
699-
}
700-
701693
/* profile details */
702694
$profiles = $this->get('compose_profiles', array());
703695
list($imap_server, $from_name, $reply_to, $from) = get_outbound_msg_profile_detail($form, $profiles, $smtp_details, $this);
704696

705-
/* xoauth2 check */
706-
smtp_refresh_oauth2_token_on_send($smtp_details, $this, $smtp_id);
707-
708697
/* adjust from and reply to addresses */
709698
list($from, $reply_to) = outbound_address_check($this, $from, $reply_to);
710699

711-
/* try to connect */
712-
$mailbox = Hm_SMTP_List::connect($smtp_id, false);
713-
if (! $mailbox || ! $mailbox->authed()) {
714-
Hm_Msgs::add("Failed to authenticate to the SMTP server", "danger");
715-
repopulate_compose_form($draft, $this);
716-
return;
717-
}
718-
719700
/* build message */
720701
$mime = new Hm_MIME_Msg($to, $subject, $body, $from, $body_type, $cc, $bcc, $in_reply_to, $from_name, $reply_to, $delivery_receipt);
721702

@@ -732,9 +713,16 @@ public function process() {
732713
}
733714

734715
/* send the message */
735-
$err_msg = $mailbox->send_message($from, $recipients, $mime->get_mime_msg(), $this->user_config->get('enable_compose_delivery_receipt_setting', false) && !empty($this->request->post['compose_delivery_receipt']));
716+
$err_msg = send_smtp_message(
717+
$this,
718+
$form['compose_smtp_id'],
719+
$from,
720+
$recipients,
721+
$mime->get_mime_msg(),
722+
$this->user_config->get('enable_compose_delivery_receipt_setting', false) && !empty($this->request->post['compose_delivery_receipt'])
723+
);
724+
736725
if ($err_msg) {
737-
Hm_Msgs::add(sprintf("%s", $err_msg), 'danger');
738726
repopulate_compose_form($draft, $this);
739727
return;
740728
}
@@ -856,14 +844,20 @@ public function process() {
856844

857845
$recipients = $reaction_mime->get_recipient_addresses();
858846

859-
$result = $this->send_reaction_message(
847+
$err_msg = send_smtp_message(
848+
$this,
860849
$selected_smtp_id,
861850
$from_address,
862851
$recipients,
863852
$full_mime_message
864853
);
865854

866-
$this->out('success', $result);
855+
if ($err_msg === false) {
856+
Hm_Msgs::add('Reaction sent successfully!', 'success');
857+
$this->out('success', true);
858+
} else {
859+
$this->out('success', false);
860+
}
867861
}
868862

869863
/**
@@ -1028,35 +1022,6 @@ private function prepare_reaction_data($reaction_emoji) {
10281022

10291023
return $reaction_content;
10301024
}
1031-
1032-
/**
1033-
* Send the reaction message via SMTP
1034-
*/
1035-
private function send_reaction_message($smtp_id, $from_address, $recipients, $mime_message) {
1036-
$smtp_details = Hm_SMTP_List::dump($smtp_id, true);
1037-
if (!$smtp_details) {
1038-
Hm_Msgs::add('Could not load selected SMTP server details.', 'danger');
1039-
return false;
1040-
}
1041-
1042-
smtp_refresh_oauth2_token_on_send($smtp_details, $this, $smtp_id);
1043-
1044-
$smtp_mailbox = Hm_SMTP_List::connect($smtp_id, false);
1045-
if (!$smtp_mailbox || !$smtp_mailbox->authed()) {
1046-
Hm_Msgs::add("Failed to authenticate to the SMTP server for sending reaction.", "danger");
1047-
return false;
1048-
}
1049-
1050-
$err_msg = $smtp_mailbox->send_message($from_address, $recipients, $mime_message);
1051-
1052-
if (!$err_msg) {
1053-
Hm_Msgs::add('Reaction sent successfully!', 'success');
1054-
return true;
1055-
} else {
1056-
Hm_Msgs::add('Failed to send reaction email.', 'danger');
1057-
return false;
1058-
}
1059-
}
10601025
}
10611026

10621027
/**

0 commit comments

Comments
 (0)