From 0bedc43e8880991b0ac3154aef100c45055b0196 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Tue, 11 Jun 2024 12:59:25 +0000 Subject: [PATCH 1/3] refactor: Email related system variables inside email section Group e-mail related system variables under an "Email settings" section. This is in preparation for the preferredMailSender implementation --- code/web/sys/SystemVariables.php | 49 +++++++++++++++++++------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/code/web/sys/SystemVariables.php b/code/web/sys/SystemVariables.php index 8c0542e5bc..50447b24b0 100644 --- a/code/web/sys/SystemVariables.php +++ b/code/web/sys/SystemVariables.php @@ -59,26 +59,35 @@ static function getObjectStructure($context = ''): array { 'description' => 'URL of the community content server', 'maxLength' => 128, ], - 'errorEmail' => [ - 'property' => 'errorEmail', - 'type' => 'text', - 'label' => 'Error Email Address', - 'description' => 'Email Address to send errors to', - 'maxLength' => 128, - ], - 'ticketEmail' => [ - 'property' => 'ticketEmail', - 'type' => 'text', - 'label' => 'Ticket Email Address', - 'description' => 'Email Address to send tickets from administrators to', - 'maxLength' => 128, - ], - 'searchErrorEmail' => [ - 'property' => 'searchErrorEmail', - 'type' => 'text', - 'label' => 'Search Error Email Address', - 'description' => 'Email Address to send errors to', - 'maxLength' => 128, + 'emailSection' => [ + 'property' => 'emailSection', + 'type' => 'section', + 'label' => 'Email Settings', + 'hideInLists' => true, + 'expandByDefault' => true, + 'properties' => [ + 'errorEmail' => [ + 'property' => 'errorEmail', + 'type' => 'text', + 'label' => 'Error Email Address', + 'description' => 'Email Address to send errors to', + 'maxLength' => 128, + ], + 'ticketEmail' => [ + 'property' => 'ticketEmail', + 'type' => 'text', + 'label' => 'Ticket Email Address', + 'description' => 'Email Address to send tickets from administrators to', + 'maxLength' => 128, + ], + 'searchErrorEmail' => [ + 'property' => 'searchErrorEmail', + 'type' => 'text', + 'label' => 'Search Error Email Address', + 'description' => 'Email Address to send errors to', + 'maxLength' => 128, + ], + ] ], 'googleBucket' => [ 'property' => 'googleBucket', From 9769036240e3da7736e0a28d766858035ea778db Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Tue, 11 Jun 2024 15:58:26 +0000 Subject: [PATCH 2/3] feat: preferredMailSender 1/2 - sortableList property This allows for a new 'sortableList' property type. This property should be a draggable list of items that, when saved, saves order they were arranged in. --- .../responsive/DataObjectUtil/property.tpl | 2 + .../DataObjectUtil/sortableList.tpl | 111 ++++++++++++++++++ code/web/sys/DataObjectUtil.php | 2 + 3 files changed, 115 insertions(+) create mode 100644 code/web/interface/themes/responsive/DataObjectUtil/sortableList.tpl diff --git a/code/web/interface/themes/responsive/DataObjectUtil/property.tpl b/code/web/interface/themes/responsive/DataObjectUtil/property.tpl index 9ff3938f71..7fd26bc4a4 100644 --- a/code/web/interface/themes/responsive/DataObjectUtil/property.tpl +++ b/code/web/interface/themes/responsive/DataObjectUtil/property.tpl @@ -569,6 +569,8 @@ {/if} {/foreach} + {elseif $property.type == 'sortableList'} + {include file="DataObjectUtil/sortableList.tpl"} {/if} diff --git a/code/web/interface/themes/responsive/DataObjectUtil/sortableList.tpl b/code/web/interface/themes/responsive/DataObjectUtil/sortableList.tpl new file mode 100644 index 0000000000..5a9d50fd92 --- /dev/null +++ b/code/web/interface/themes/responsive/DataObjectUtil/sortableList.tpl @@ -0,0 +1,111 @@ +{strip} +
    + {foreach from=$property.values item=propertyName key=propertyValue} +
  1. {$propertyName|escape}
  2. + {/foreach} +
+ + + \ No newline at end of file diff --git a/code/web/sys/DataObjectUtil.php b/code/web/sys/DataObjectUtil.php index 8b907e2b90..7c34222c3f 100644 --- a/code/web/sys/DataObjectUtil.php +++ b/code/web/sys/DataObjectUtil.php @@ -575,6 +575,8 @@ static function processProperty(DataObject $object, $property) { } $object->$propertyName = $values; + } elseif ($property['type'] == 'sortableList') { + $object->setProperty($propertyName, $_REQUEST[$propertyName], $property); } } } \ No newline at end of file From 1fd01b39cb7d7bec2cc336bafc22cfbf807369b6 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Tue, 11 Jun 2024 15:58:39 +0000 Subject: [PATCH 3/3] feat: preferredMailSender 2/2 - preferredMailSender system variable This new preferredMailSender system variable defaults to 'SMTP|AmazonSES|SendGrid" which means it will first look for an SMTP setting when sending email directly from Aspen. If that fails or doesn't exist, it'll then query Amazon SES, and finally SendGrid. This order may be configured through the system variables UI. Test the UI: Drag and drop the options, reorder them, save. Refresh. Verify all is working as intended. Test the functionality: Create a new SMTP setting and attempt sending an email (i.e. through the 'Submit a ticket' feature). Then, create a new Amazon SES setting and reorder the preferredMailSender. Notice it should use the first one selected in priority. --- .../version_updates/24.06.00.php | 10 +++++ code/web/sys/Email/Mailer.php | 42 +++++++++++++------ code/web/sys/SystemVariables.php | 12 ++++++ install/aspen.sql | 1 + 4 files changed, 53 insertions(+), 12 deletions(-) diff --git a/code/web/sys/DBMaintenance/version_updates/24.06.00.php b/code/web/sys/DBMaintenance/version_updates/24.06.00.php index aaf312cc9a..b175e0edd3 100644 --- a/code/web/sys/DBMaintenance/version_updates/24.06.00.php +++ b/code/web/sys/DBMaintenance/version_updates/24.06.00.php @@ -172,6 +172,16 @@ function getUpdates24_06_00(): array { ], ], //permissions_create_administer_smtp + + 'systemVariables_preferredMailSender' => [ + 'title' => 'System Variables - Preferred Mail Sender', + 'description' => 'Allow sortable configuration of which mail sender to use by order of priority.', + 'sql' => [ + "alter table system_variables ADD COLUMN preferredMailSender varchar(128) DEFAULT 'SMTP|AmazonSES|SendGrid'", + ], + ], + //systemVariables_preferredMailSender + //other diff --git a/code/web/sys/Email/Mailer.php b/code/web/sys/Email/Mailer.php index e4fbd2e53b..fe2c12cfa9 100644 --- a/code/web/sys/Email/Mailer.php +++ b/code/web/sys/Email/Mailer.php @@ -22,20 +22,17 @@ public function send($to, $subject, $body, $replyTo = null, $htmlBody = null, $a require_once ROOT_DIR . '/sys/Email/AmazonSesSetting.php'; require_once ROOT_DIR . '/sys/Email/SMTPSetting.php'; require_once ROOT_DIR . '/sys/CurlWrapper.php'; + require_once ROOT_DIR . '/sys/SystemVariables.php'; //TODO: Do validation of the address - $amazonSesSettings = new AmazonSesSetting(); - $smtpServerSettings = new SMTPSetting(); - if($smtpServerSettings->find(true)) { - $result = $this->sendViaSMTP($smtpServerSettings, $to, $replyTo, $subject, $body, $htmlBody, $attachments); - }elseif ($amazonSesSettings->find(true)) { - $result = $this->sendViaAmazonSes($amazonSesSettings, $to, $replyTo, $subject, $body, $htmlBody, $attachments); - } else { - $sendGridSettings = new SendGridSetting(); - if ($sendGridSettings->find(true)) { - $result = $this->sendViaSendGrid($sendGridSettings, $to, $replyTo, $subject, $body, $htmlBody); - } else { - $result = false; + $systemVariables = new SystemVariables(); + if ($systemVariables->find(true) && !empty($systemVariables->preferredMailSender)) { + $mailSenders = explode("|", $systemVariables->preferredMailSender); + foreach ($mailSenders as $mailSender){ + $result = $this->sendViaAbstractSender($mailSender, $to, $replyTo, $subject, $body, $htmlBody, $attachments); + if ($result) { + break; + } } } @@ -145,4 +142,25 @@ private function sendViaAmazonSes(AmazonSesSetting $amazonSesSettings, string $t private function sendViaSMTP(SMTPSetting $smtpSettings, string $to, ?string $replyTo, string $subject, ?string $body, ?string $htmlBody, ?array $attachments): bool { return $smtpSettings->sendEmail($to, $replyTo, $subject, $body, $htmlBody, $attachments); } + + private function sendViaAbstractSender($mailSender, $to, $replyTo, $subject, $body, $htmlBody, $attachments) { + $result = false; + if($mailSender == 'AmazonSES') { + $amazonSesSettings = new AmazonSesSetting(); + if($amazonSesSettings->find(true)){ + $result = $this->sendViaAmazonSes($amazonSesSettings, $to, $replyTo, $subject, $body, $htmlBody, $attachments); + } + } else if($mailSender == 'SendGrid') { + $sendGridSettings = new SendGridSetting(); + if($sendGridSettings->find(true)){ + $result = $this->sendViaSendGrid($sendGridSettings, $to, $replyTo, $subject, $body, $htmlBody); + } + } else if($mailSender == 'SMTP') { + $smtpSettings = new SMTPSetting(); + if($smtpSettings->find(true)){ + $result = $this->sendViaSMTP($smtpSettings, $to, $replyTo, $subject, $body, $htmlBody, $attachments); + } + } + return $result; + } } \ No newline at end of file diff --git a/code/web/sys/SystemVariables.php b/code/web/sys/SystemVariables.php index 50447b24b0..82ab838db9 100644 --- a/code/web/sys/SystemVariables.php +++ b/code/web/sys/SystemVariables.php @@ -7,6 +7,7 @@ class SystemVariables extends DataObject { public $errorEmail; public $ticketEmail; public $searchErrorEmail; + public $preferredMailSender; public $loadCoversFrom020z; public $currencyCode; public $runNightlyFullIndex; @@ -87,6 +88,17 @@ static function getObjectStructure($context = ''): array { 'description' => 'Email Address to send errors to', 'maxLength' => 128, ], + 'preferredMailSender' => [ + 'property' => 'preferredMailSender', + 'type' => 'sortableList', + 'values' => [ + 'AmazonSES' => 'AmazonSES', + 'SendGrid' => 'SendGrid', + 'SMTP' => 'SMTP', + ], + 'label' => 'Preferred Mail Sender Order', + 'description' => 'Sort order of preferred mail sender', + ], ] ], 'googleBucket' => [ diff --git a/install/aspen.sql b/install/aspen.sql index b96acb6881..98f86fd97f 100644 --- a/install/aspen.sql +++ b/install/aspen.sql @@ -4949,6 +4949,7 @@ CREATE TABLE `system_variables` ( `errorEmail` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `ticketEmail` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `searchErrorEmail` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, + `preferredMailSender` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT 'SMTP|AmazonSES|SendGrid', `loadCoversFrom020z` tinyint(1) DEFAULT '0', `runNightlyFullIndex` tinyint(1) DEFAULT '0', `currencyCode` char(3) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT 'USD',