0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-22 14:39:45 -05:00

♻️ Refactor email sending.

This commit is contained in:
Andrey Antukh 2020-04-20 13:44:42 +02:00
parent 6ba3a28143
commit 7fba483bf1
10 changed files with 87 additions and 68 deletions

View file

@ -4,8 +4,7 @@
"jcenter" {:url "https://jcenter.bintray.com/"}}
:deps
{org.clojure/clojure {:mvn/version "1.10.1"}
funcool/promesa {:mvn/version "5.1.0"}
funcool/cuerdas {:mvn/version "2020.03.26-3"}
org.clojure/data.json {:mvn/version "1.0.0"}
;; Logging
org.clojure/tools.logging {:mvn/version "0.5.0"}
@ -13,7 +12,7 @@
org.apache.logging.log4j/log4j-core {:mvn/version "2.13.0"}
org.apache.logging.log4j/log4j-jul {:mvn/version "2.13.0"}
org.apache.logging.log4j/log4j-slf4j-impl {:mvn/version "2.13.0"}
funcool/datoteka {:mvn/version "1.2.0"}
expound/expound {:mvn/version "0.8.4"}
instaparse/instaparse {:mvn/version "1.4.10"}
com.cognitect/transit-clj {:mvn/version "0.8.319"}
@ -21,23 +20,26 @@
;; TODO: vendorize pgclient under `vertx-clojure/vertx-pgclient`
io.vertx/vertx-pg-client {:mvn/version "4.0.0-milestone4"}
io.lettuce/lettuce-core {:mvn/version "5.2.2.RELEASE"}
java-http-clj/java-http-clj {:mvn/version "0.4.1"}
vertx-clojure/vertx
{:local/root "vendor/vertx"
:deps/manifest :pom}
funcool/datoteka {:mvn/version "1.2.0"}
funcool/promesa {:mvn/version "5.1.0"}
funcool/cuerdas {:mvn/version "2020.03.26-3"}
funcool/sodi
{:local/root "vendor/sodi"
:deps/manifest :pom}
lambdaisland/uri {:mvn/version "1.1.0"}
lambdaisland/uri {:mvn/version "1.1.0"
:exclusions [org.clojure/data.json]}
danlentz/clj-uuid {:mvn/version "0.1.9"}
org.jsoup/jsoup {:mvn/version "1.12.1"}
org.im4java/im4java {:mvn/version "1.4.0"}
org.lz4/lz4-java {:mvn/version "1.7.1"}
com.github.spullara.mustache.java/compiler {:mvn/version "0.9.6"}
commons-io/commons-io {:mvn/version "2.6"}
com.draines/postal {:mvn/version "2.0.3"

View file

@ -31,8 +31,11 @@
:assets-directory "resources/public/static"
:media-uri "http://localhost:6060/media/"
:assets-uri "http://localhost:6060/static/"
:email-reply-to "no-reply@nodomain.com"
:email-from "no-reply@nodomain.com"
:sendmail-backend "console"
:sendmail-reply-to "no-reply@example.com"
:sendmail-from "no-reply@example.com"
:smtp-enabled false
:allow-demo-users true
:registration-enabled true
@ -51,8 +54,10 @@
(s/def ::assets-directory ::us/string)
(s/def ::media-uri ::us/string)
(s/def ::media-directory ::us/string)
(s/def ::email-reply-to ::us/email)
(s/def ::email-from ::us/email)
(s/def ::sendmail-backend ::us/string)
(s/def ::sendmail-backend-apikey ::us/string)
(s/def ::sendmail-reply-to ::us/email)
(s/def ::sendmail-from ::us/email)
(s/def ::smtp-host ::us/string)
(s/def ::smtp-port ::us/integer)
(s/def ::smtp-user (s/nilable ::us/string))
@ -76,8 +81,10 @@
::assets-uri
::media-directory
::media-uri
::email-reply-to
::email-from
::sendmail-reply-to
::sendmail-from
::sendmail-backend
::sendmail-backend-apikey
::smtp-host
::smtp-port
::smtp-user

View file

@ -34,16 +34,16 @@
(defn send!
"Schedule the email for sending."
([email context] (send! db/pool email context))
([conn email context]
(us/verify fn? email)
([conn email-factory context]
(us/verify fn? email-factory)
(us/verify map? context)
(let [defaults {:from (:email-from cfg/config)
:reply-to (:email-reply-to cfg/config)}
data (->> (merge defaults context)
(email))]
(let [defaults {:from (:sendmail-from cfg/config)
:reply-to (:sendmail-reply-to cfg/config)}
data (merge defaults context)
email (email-factory data)]
(tasks/schedule! conn {:name "sendmail"
:delay 0
:props data}))))
:props email}))))
;; --- Emails
@ -62,4 +62,3 @@
(def password-recovery
"A password recovery notification email."
(emails/build ::password-recovery default-context))

View file

@ -117,8 +117,7 @@
(p/then decode-task-row)
(p/then (fn [item]
(when item
(log/info "Execute task" (:name item)
"with props" (pr-str (:props item)))
(log/info "Execute task" (:name item))
(-> (p/do! (handle-task tasks item))
(p/handle (fn [v e]
(if e

View file

@ -5,33 +5,20 @@
;; This Source Code Form is "Incompatible With Secondary Licenses", as
;; defined by the Mozilla Public License, v. 2.0.
;;
;; Copyright (c) 2020 Andrey Antukh <niwi@niwi.nz>
;; Copyright (c) 2020 UXBOX Labs SL
(ns uxbox.tasks.sendmail
"Email sending jobs."
(:require
[clojure.data.json :as json]
[clojure.tools.logging :as log]
[cuerdas.core :as str]
[postal.core :as postal]
[vertx.util :as vu]
[promesa.core :as p]
[uxbox.common.exceptions :as ex]
[uxbox.config :as cfg]
[uxbox.core :refer [system]]
[uxbox.util.blob :as blob]))
[uxbox.util.http :as http]))
(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)
:enabled (:smtp-enabled config)})
(defmulti sendmail (fn [config email] (:sendmail-backend config)))
(defn- send-email-to-console
[email]
(defmethod sendmail "console"
[config email]
(let [out (with-out-str
(println "email console dump:")
(println "******** start email" (:id email) "**********")
@ -40,28 +27,41 @@
(println " reply-to: " (:reply-to email))
(println " subject: " (:subject email))
(println " content:")
(doseq [item (rest (:body email))]
(when (str/starts-with? (:type item) "text/plain")
(println (:content item))))
(doseq [item (:content email)]
(when (= (:type item) "text/plain")
(println (:value item))))
(println "******** end email "(:id email) "**********"))]
(log/info out)
{:error :SUCCESS}))
(log/info out)))
(defn send-email
[email]
(vu/blocking
(let [config (get-smtp-config cfg/config)
result (if (:enabled config)
(postal/send-message config email)
(send-email-to-console email))]
(when (not= (:error result) :SUCCESS)
(ex/raise :type :sendmail-error
:code :email-not-sent
:context result))
nil)))
(defmethod sendmail "sendgrid"
[config email]
(let [apikey (:sendmail-backend-apikey config)
dest (mapv #(array-map :email %) (:to email))
params {:personalizations [{:to dest
:subject (:subject email)}]
:from {:email (:from email)}
:reply_to {:email (:reply-to email)}
:content (:content email)}
headers {"Authorization" (str "Bearer " apikey)
"Content-Type" "application/json"}
body (json/write-str params)]
(-> (http/send! {:method :post
: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))
(= 202 (:status response))
nil
:else
(log/error "Unexpected status from sendgrid:" (pr-str response))))))))
(defn handler
{:uxbox.tasks/name "sendmail"}
[{:keys [props] :as task}]
(send-email props))
(sendmail cfg/config props))

View file

@ -30,7 +30,7 @@
"eol = ('\\n' | '\\r\\n'); "))
(def ^:private parse-fn (insta/parser grammar))
(def ^:private email-path "emails/%(lang)s/%(id)s.mustache")
(def ^:private email-path "emails/%(id)s/%(lang)s.mustache")
(defn- parse-template
[content]
@ -49,7 +49,8 @@
(s/def ::body-html string?)
(s/def ::parsed-email
(s/keys :req-un [::subject ::body-html ::body-html]))
(s/keys :req-un [::subject ::body-text]
:opt-un [::body-html]))
(defn- build-base-email
[data context]
@ -59,11 +60,11 @@
:hint "Seems like the email template has invalid data."
:contex data))
{:subject (:subject data)
:body [:alternative
{:type "text/plain; charset=utf-8"
:content (:body-text data)}
{:type "text/html; charset=utf-8"
:content (:body-html data)}]})
:content (cond-> []
(:body-text data) (conj {:type "text/plain"
:value (:body-text data)})
(:body-html data) (conj {:type "text/html"
:value (:body-html data)}))})
(defn- impl-build-email
[id context]
@ -102,6 +103,6 @@
:hint "seems like the template is wrong or does not exists."
::id id))
(cond-> (assoc email :id (name id))
(:to context) (assoc :to (:to context))
(:to context) (assoc :to [(:to context)])
(:from context) (assoc :from (:from context))
(:reply-to context) (assoc :reply-to (:reply-to context)))))))

View file

@ -5,4 +5,15 @@
;; Copyright (c) 2019 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.util.http
"Http related helpers.")
"Http client abstraction layer."
(:require
[promesa.core :as p]
[promesa.exec :as px]
[java-http-clj.core :as http]))
(def default-client
(delay (http/build-client {:executor @px/default-executor})))
(defn send!
[req]
(http/send-async req {:client @default-client :as :string}))