From 428bd42f15b93b5f891dbdfe5f72d4a0bd98b42f Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 20 Apr 2020 14:08:33 +0200 Subject: [PATCH] :recycle: Add smtp backend for sendmail task. --- backend/src/uxbox/config.clj | 3 -- backend/src/uxbox/tasks/sendmail.clj | 54 +++++++++++++++++++++++----- docker/devenv/docker-compose.yaml | 3 ++ 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/backend/src/uxbox/config.clj b/backend/src/uxbox/config.clj index 461f60aa97..2d8f135499 100644 --- a/backend/src/uxbox/config.clj +++ b/backend/src/uxbox/config.clj @@ -36,7 +36,6 @@ :sendmail-reply-to "no-reply@example.com" :sendmail-from "no-reply@example.com" - :smtp-enabled false :allow-demo-users true :registration-enabled true :registration-domain-whitelist "" @@ -64,7 +63,6 @@ (s/def ::smtp-password (s/nilable ::us/string)) (s/def ::smtp-tls ::us/boolean) (s/def ::smtp-ssl ::us/boolean) -(s/def ::smtp-enabled ::us/boolean) (s/def ::allow-demo-users ::us/boolean) (s/def ::registration-enabled ::us/boolean) (s/def ::registration-domain-whitelist ::us/string) @@ -91,7 +89,6 @@ ::smtp-password ::smtp-tls ::smtp-ssl - ::smtp-enabled ::debug-humanize-transit ::allow-demo-users ::registration-enabled])) diff --git a/backend/src/uxbox/tasks/sendmail.clj b/backend/src/uxbox/tasks/sendmail.clj index 0e15880587..95cb3f4375 100644 --- a/backend/src/uxbox/tasks/sendmail.clj +++ b/backend/src/uxbox/tasks/sendmail.clj @@ -11,9 +11,13 @@ (:require [clojure.data.json :as json] [clojure.tools.logging :as log] + [postal.core :as postal] [promesa.core :as p] + [uxbox.common.data :as d] + [uxbox.common.exceptions :as ex] [uxbox.config :as cfg] - [uxbox.util.http :as http])) + [uxbox.util.http :as http] + [vertx.util :as vu])) (defmulti sendmail (fn [config email] (:sendmail-backend config))) @@ -49,16 +53,48 @@ :headers headers :uri "https://api.sendgrid.com/v3/mail/send" :body body}) - (p/handle (fn [response error] - (cond - error - (log/error "Error on sending email to sendgrid:" (pr-str error)) + (p/handle + (fn [response error] + (cond + error + (log/error "Error on sending email to sendgrid:" (pr-str error)) - (= 202 (:status response)) - nil + (= 202 (:status response)) + nil - :else - (log/error "Unexpected status from sendgrid:" (pr-str response)))))))) + :else + (log/error "Unexpected status from sendgrid:" (pr-str response)))))))) + +(defn- get-smtp-config + [config] + {:host (:smtp-host config) + :port (:smtp-port config) + :user (:smtp-user config) + :pass (:smtp-password config) + :ssl (:smtp-ssl config) + :tls (:smtp-tls config)}) + +(defn- email->postal + [email] + {:from (:from email) + :to (:to email) + :subject (:subject email) + :body (d/concat [:alternative] + (map (fn [{:keys [type value]}] + {:type (str type "; charset=utf-8") + :content value}) + (:content email)))}) + +(defmethod sendmail "smtp" + [config email] + (vu/blocking + (let [config (get-smtp-config config) + email (email->postal email) + result (postal/send-message config email)] + (when (not= (:error result) :SUCCESS) + (ex/raise :type :sendmail-error + :code :email-not-sent + :context result))))) (defn handler {:uxbox.tasks/name "sendmail"} diff --git a/docker/devenv/docker-compose.yaml b/docker/devenv/docker-compose.yaml index aa36571d3f..e773d4bc48 100644 --- a/docker/devenv/docker-compose.yaml +++ b/docker/devenv/docker-compose.yaml @@ -42,6 +42,9 @@ services: - UXBOX_DATABASE_URI=postgresql://postgres/uxbox - UXBOX_DATABASE_USERNAME=uxbox - UXBOX_DATABASE_PASSWORD=uxbox + - UXBOX_SENDMAIL_BACKEND=smtp + - UXBOX_SMTP_HOST=smtp + - UXBOX_SMTP_PORT=25 smtp: container_name: "uxbox-devenv-smtp"