Skip to content

Commit

Permalink
feat: preferredMailSender 2/2 - preferredMailSender system variable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ammopt committed Jun 12, 2024
1 parent 4a188f4 commit 0090490
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
10 changes: 10 additions & 0 deletions code/web/sys/DBMaintenance/version_updates/24.06.00.php
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
42 changes: 30 additions & 12 deletions code/web/sys/Email/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -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;
}
}
12 changes: 12 additions & 0 deletions code/web/sys/SystemVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class SystemVariables extends DataObject {
public $errorEmail;
public $ticketEmail;
public $searchErrorEmail;
public $preferredMailSender;
public $loadCoversFrom020z;
public $currencyCode;
public $runNightlyFullIndex;
Expand Down Expand Up @@ -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' => [
Expand Down
1 change: 1 addition & 0 deletions install/aspen.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 0090490

Please sign in to comment.