From 2ad9ce865a702f81d3fdd4f4ddb6a0df84ccbc2b Mon Sep 17 00:00:00 2001 From: Benn Oshrin Date: Sun, 11 Jan 2026 09:41:57 -0500 Subject: [PATCH] Inherit SMTP server from COmanage CO (CFM-80) --- .../templates/MatchServerAttributes/fields.inc | 1 - app/resources/locales/en_US/error.po | 3 +++ app/resources/locales/en_US/field.po | 3 +++ app/src/Lib/Util/DeliveryUtilities.php | 12 +++++++++--- app/src/Model/Table/CoSettingsTable.php | 12 ++++++++++++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/app/plugins/CoreServer/templates/MatchServerAttributes/fields.inc b/app/plugins/CoreServer/templates/MatchServerAttributes/fields.inc index b4296df54..bbef08ca1 100644 --- a/app/plugins/CoreServer/templates/MatchServerAttributes/fields.inc +++ b/app/plugins/CoreServer/templates/MatchServerAttributes/fields.inc @@ -56,7 +56,6 @@ $fields[] = 'required'; var curtypeid = document.getElementById(field); var typeid = document.getElementById('type-id'); - alert(field + ": " + curtypeid.value); typeid.value = curtypeid.value; } diff --git a/app/resources/locales/en_US/error.po b/app/resources/locales/en_US/error.po index 85e84512f..9cd3bb702 100644 --- a/app/resources/locales/en_US/error.po +++ b/app/resources/locales/en_US/error.po @@ -424,6 +424,9 @@ msgstr "Ooops... Something went wrong." msgid "setup.co.comanage" msgstr "Failed to setup COmanage CO" +msgid "smtp_server.none" +msgstr "No outgoing SMTP Server configuration found" + msgid "tree.parent.invalid" msgstr "The selected {0} is not a valid parent for the current record" diff --git a/app/resources/locales/en_US/field.po b/app/resources/locales/en_US/field.po index 9421bbfcd..64c18c174 100644 --- a/app/resources/locales/en_US/field.po +++ b/app/resources/locales/en_US/field.po @@ -447,6 +447,9 @@ msgstr "If set, when sending email only verified Email Addresses of this type (a msgid "CoSettings.email_smtp_server_id" msgstr "Outgoing SMTP Server" +msgid "CoSettings.email_smtp_server_id.desc" +msgstr "If a Platform level SMTP Server is set, it will be used when no CO-specific SMTP Server is configured" + msgid "CoSettings.permitted_fields_name" msgstr "Name Permitted Fields" diff --git a/app/src/Lib/Util/DeliveryUtilities.php b/app/src/Lib/Util/DeliveryUtilities.php index 66039d9d3..4d63b3a17 100644 --- a/app/src/Lib/Util/DeliveryUtilities.php +++ b/app/src/Lib/Util/DeliveryUtilities.php @@ -41,7 +41,8 @@ class DeliveryUtilities { use \App\Lib\Traits\LabeledLogTrait; /** - * Send an email to an Address. + * Send an email to an Address. If no Outgoing SMTP Server is configured, + * an InvalidArgumentException will be thrown. * * @since COmanage Registry v5.0.0 * @param int $coId CO ID @@ -52,8 +53,9 @@ class DeliveryUtilities { * @param string $cc Addresses to cc * @param string $bcc Addresses to bcc * @param string $replyTo Reply-To address to use, instead of the default - * @return bool Returns true if mail was sent, false if no SMTP server was set + * @return bool Returns true if mail was sent * @throws Cake\Network\Exception\SocketException + * @throws InvalidArgumentException */ public static function sendEmailToAddress( @@ -78,7 +80,11 @@ public static function sendEmailToAddress( // No SMTP configured self::slog('debug', "No Outgoing SMTP Server is configured for CO $coId, so no mail will be sent"); - return false; + // Originally we returned false here, but that means the only indication of a missing + // SMTP server configuration is in the error logs, which doesn't seem helpful. + // So we throw an Exception instead, and if the calling code doesn't like that + // it can catch it and do something else. + throw new \InvalidArgumentException(__d('error', 'smtp_server.none')); } // Next figure out the recipient diff --git a/app/src/Model/Table/CoSettingsTable.php b/app/src/Model/Table/CoSettingsTable.php index 3d7680659..3f9a08a72 100644 --- a/app/src/Model/Table/CoSettingsTable.php +++ b/app/src/Model/Table/CoSettingsTable.php @@ -349,6 +349,18 @@ public function getSmtpServer(int $coId): ?\CoreServer\Model\Entity\SmtpServer { ->firstOrFail(); } + // If this isn't the COmanageCO, then use that configuration, if there is one + $COmanageCO = $this->Cos->find('COmanageCO')->firstOrFail(); + + if($COmanageCO->id != $coId) { + // Note we're returning an object outside the calling CO's space, which isn't + // expected this shouldn't be a problem ordinarily (core code should know what + // it's doing, and plugins have full access to the database anyway), but there + // could be unidentified edge cases where this could cause problems. + + return $this->getSmtpServer($COmanageCO->id); + } + return null; }