Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

fix(channel): count the custom SMTP server in email-configured? and the send guard (#84449)

Aleksandr Lesnenko6b6f1c4bc48d
diff --git a/src/metabase/channel/email.clj b/src/metabase/channel/email.clj
index 2955a2d7c61..6ede1004476 100644
--- a/src/metabase/channel/email.clj
+++ b/src/metabase/channel/email.clj
@@ -6,7 +6,6 @@
[metabase.analytics-interface.core :as analytics]
[metabase.channel.settings :as channel.settings]
[metabase.permissions.core :as perms]
- [metabase.premium-features.core :as premium-features]
[metabase.settings.core :as setting]
[metabase.util :as u]
[metabase.util.i18n :refer [tru]]
@@ -105,7 +104,7 @@
{})))
(defn- smtp-settings []
- (merge (if (and (channel.settings/smtp-override-enabled) (premium-features/is-hosted?))
+ (merge (if (channel.settings/smtp-override-active?)
(-> {:host (channel.settings/email-smtp-host-override)
:user (channel.settings/email-smtp-username-override)
:pass (channel.settings/email-smtp-password-override)
@@ -159,7 +158,7 @@
semantics in [[send-email-retrying!]]."
[{:keys [subject recipients message-type message bcc?] :as _email}]
(try
- (when-not (channel.settings/email-smtp-host)
+ (when-not (channel.settings/email-configured?)
(throw (ex-info (tru "SMTP host is not set.") {:cause :smtp-host-not-set})))
;; Now send the email
(let [to-type (if bcc? :bcc :to)
diff --git a/src/metabase/channel/rest/api/email.clj b/src/metabase/channel/rest/api/email.clj
index 2ca93a3d132..5d402ebe210 100644
--- a/src/metabase/channel/rest/api/email.clj
+++ b/src/metabase/channel/rest/api/email.clj
@@ -6,7 +6,8 @@
[metabase.channel.email :as email]
[metabase.channel.settings :as channel.settings]
[metabase.permissions.core :as perms]
- [metabase.settings.core :as setting]))
+ [metabase.settings.core :as setting]
+ [metabase.util.i18n :refer [tru]]))
(set! *warn-on-reflection* true)
@@ -53,15 +54,15 @@
Returns `{:ok true}` if we were able to send the message successfully, otherwise a standard 400 error response."
[]
(perms/check-has-application-permission :setting)
- (when-not (and (channel.settings/email-smtp-port) (channel.settings/email-smtp-host))
+ (if-not (channel.settings/email-configured?)
{:status 400
- :body "Wrong host or port"})
- (let [response (email/send-message-or-throw!
- {:subject "Metabase Test Email"
- :recipients [(:email @api/*current-user*)]
- :message-type :text
- :message "Your Metabase emails are working — hooray!"})]
- (if-not (::email/error response)
- {:ok true}
- {:status 400
- :body (email/humanize-error-messages mb-to-smtp-settings response)})))
+ :body (tru "SMTP host is not set.")}
+ (let [response (email/send-message-or-throw!
+ {:subject "Metabase Test Email"
+ :recipients [(:email @api/*current-user*)]
+ :message-type :text
+ :message "Your Metabase emails are working — hooray!"})]
+ (if-not (::email/error response)
+ {:ok true}
+ {:status 400
+ :body (email/humanize-error-messages mb-to-smtp-settings response)}))))
diff --git a/src/metabase/channel/settings.clj b/src/metabase/channel/settings.clj
index 7fee088266d..419917f0952 100644
--- a/src/metabase/channel/settings.clj
+++ b/src/metabase/channel/settings.clj
@@ -3,6 +3,7 @@
[clojure.string :as str]
[java-time.api :as t]
[metabase.config.core :as config]
+ [metabase.premium-features.core :as premium-features]
[metabase.settings.core :as setting :refer [defsetting]]
[metabase.startup.core :as startup]
[metabase.util.http :as u.http]
@@ -283,6 +284,11 @@
(throw (ex-info (tru "Cannot enable smtp-override when it is not configured.") {:status-code 400})))
(setting/set-value-of-type! :boolean :smtp-override-enabled new-value)))
+(defn smtp-override-active?
+ "Whether email goes out through the custom SMTP server instead of the primary one."
+ []
+ (and (smtp-override-enabled) (premium-features/is-hosted?)))
+
(defsetting email-max-recipients-per-second
(deferred-tru "The maximum number of recipients, summed across emails, that can be sent per second.
Note that the final email sent before reaching the limit is able to exceed it, if it has multiple recipients.")
@@ -306,7 +312,7 @@
:type :boolean
:visibility :public
:setter :none
- :getter #(boolean (email-smtp-host))
+ :getter #(boolean (if (smtp-override-active?) (email-smtp-host-override) (email-smtp-host)))
:doc false)
(defsetting surveys-enabled
diff --git a/test/metabase/channel/email_test.clj b/test/metabase/channel/email_test.clj
index 49840ce7db5..c44c93c5175 100644
--- a/test/metabase/channel/email_test.clj
+++ b/test/metabase/channel/email_test.clj
@@ -424,6 +424,27 @@
:message "101. Metabase will make you a better person")
(@inbox "[email protected]"))))))))))
+(deftest send-message-or-throw!-smtp-host-test
+ (premium-features.test-util/with-premium-features [:cloud-custom-smtp]
+ (dynamic-redefs/with-dynamic-fn-redefs [premium-features/is-hosted? (constantly true)]
+ (let [sent-hosts (atom [])
+ send! #(email/send-message-or-throw! {:subject "Subject"
+ :recipients ["[email protected]"]
+ :message-type :text
+ :message "Message"})]
+ (dynamic-redefs/with-dynamic-fn-redefs [email/send-email! (fn [smtp-settings _] (swap! sent-hosts conj (:host smtp-settings)))]
+ (tu/with-temporary-setting-values [email-smtp-host nil
+ email-smtp-host-override "cloud.metabase.com"
+ smtp-override-enabled true]
+ (testing "sends through the custom SMTP server when it is the only one configured"
+ (send!)
+ (is (= ["cloud.metabase.com"] @sent-hosts)))
+ (testing "sends nothing when the custom SMTP server is disabled and no primary host is set"
+ (reset! sent-hosts [])
+ (tu/with-temporary-setting-values [smtp-override-enabled false]
+ (send!))
+ (is (= [] @sent-hosts)))))))))
+
(deftest throttle-test
;; The throttle is applied once per logical message in send-message-or-throw!; check-email-throttle is the unit
;; that does the recipient accounting, so we exercise it directly (no actual sending involved).
diff --git a/test/metabase/channel/rest/api/email_test.clj b/test/metabase/channel/rest/api/email_test.clj
index dbcf32f71e1..4078a7b3dee 100644
--- a/test/metabase/channel/rest/api/email_test.clj
+++ b/test/metabase/channel/rest/api/email_test.clj
@@ -2,6 +2,7 @@
(:require
[clojure.test :refer :all]
[metabase.channel.email :as email]
+ [metabase.premium-features.core :as premium-features]
[metabase.settings.core :as setting]
[metabase.test :as mt]
[metabase.test.util :as tu]))
@@ -47,6 +48,27 @@
:body "Your Metabase emails are working — hooray!"}]}
@mt/inbox)))))))
+(deftest test-email-without-smtp-server-test
+ (testing "POST /api/email/test refuses to send when no SMTP server is configured"
+ (mt/with-temp-env-var-value! [MB_EMAIL_SMTP_HOST nil]
+ (mt/with-temporary-setting-values [email-smtp-host nil]
+ (is (= "SMTP host is not set."
+ (mt/user-http-request :crowberto :post 400 "email/test")))))))
+
+(deftest test-email-through-smtp-override-test
+ (testing "POST /api/email/test sends through the custom SMTP server when it is the only one configured"
+ (mt/with-premium-features [:cloud-custom-smtp]
+ (mt/with-dynamic-fn-redefs [premium-features/is-hosted? (constantly true)]
+ (mt/with-temp-env-var-value! [MB_EMAIL_SMTP_HOST nil]
+ (let [sent-hosts (atom [])]
+ (mt/with-dynamic-fn-redefs [email/send-email! (fn [smtp-settings _] (swap! sent-hosts conj (:host smtp-settings)))]
+ (mt/with-temporary-setting-values [email-smtp-host nil
+ email-smtp-host-override "cloud.metabase.com"
+ smtp-override-enabled true]
+ (is (= {:ok true}
+ (mt/user-http-request :crowberto :post 200 "email/test")))
+ (is (= ["cloud.metabase.com"] @sent-hosts))))))))))
+
(deftest update-email-settings-test
;; There is a lot of overlap with the /api/email/override test, but enough differences that we keep them separate.
;; NOTE: When adding tests, ask yourself "should this also be tested in the /api/email/override test?"
diff --git a/test/metabase/channel/settings_test.clj b/test/metabase/channel/settings_test.clj
index 65b599833bb..2f1b4fcd47b 100644
--- a/test/metabase/channel/settings_test.clj
+++ b/test/metabase/channel/settings_test.clj
@@ -3,6 +3,7 @@
(:require
[clojure.test :refer :all]
[metabase.channel.settings :as channel.settings]
+ [metabase.premium-features.core :as premium-features]
[metabase.settings.core :as setting]
[metabase.startup.core :as startup]
[metabase.test :as mt])
@@ -83,6 +84,25 @@
email-smtp-host-override "localhost"]
(is (= "true" (channel.settings/smtp-override-enabled! true)))))))
+(deftest email-configured?-test
+ (mt/with-premium-features [:cloud-custom-smtp]
+ (mt/with-dynamic-fn-redefs [premium-features/is-hosted? (constantly true)]
+ (mt/with-temporary-setting-values [email-smtp-host nil
+ email-smtp-host-override "cloud.metabase.com"
+ smtp-override-enabled true]
+ (testing "the custom SMTP server alone counts as configured on a hosted instance"
+ (is (true? (channel.settings/email-configured?))))
+ (testing "a disabled custom SMTP server does not count"
+ (mt/with-temporary-setting-values [smtp-override-enabled false]
+ (is (false? (channel.settings/email-configured?)))))
+ (testing "the custom SMTP server does not count on a self-hosted instance"
+ (mt/with-dynamic-fn-redefs [premium-features/is-hosted? (constantly false)]
+ (is (false? (channel.settings/email-configured?)))))
+ (testing "the primary SMTP server counts when the custom one is disabled"
+ (mt/with-temporary-setting-values [email-smtp-host "smtp.example.com"
+ smtp-override-enabled false]
+ (is (true? (channel.settings/email-configured?)))))))))
+
(deftest find-cached-slack-channel-or-username-test
(let [channels [{:display-name "#general" :name "general" :id "C001"}
{:display-name "#data-team" :name "data-team" :id "C002"}