mirror of
https://github.com/penpot/penpot.git
synced 2025-05-21 12:36:13 +02:00
♻️ Refactor email sending subsystem.
This commit is contained in:
parent
132e99ab71
commit
5b9c596170
5 changed files with 272 additions and 159 deletions
|
@ -9,91 +9,42 @@
|
|||
|
||||
(ns app.tasks.sendmail
|
||||
(:require
|
||||
[clojure.data.json :as json]
|
||||
[clojure.tools.logging :as log]
|
||||
[postal.core :as postal]
|
||||
[app.common.data :as d]
|
||||
[app.common.exceptions :as ex]
|
||||
[app.util.emails :as emails]
|
||||
[app.config :as cfg]
|
||||
[app.metrics :as mtx]
|
||||
[app.util.http :as http]))
|
||||
[app.metrics :as mtx]))
|
||||
|
||||
(defmulti sendmail (fn [config email] (:sendmail-backend config)))
|
||||
(defn- send-console!
|
||||
[cfg email]
|
||||
(let [baos (java.io.ByteArrayOutputStream.)
|
||||
mesg (emails/smtp-message cfg email)]
|
||||
(.writeTo mesg baos)
|
||||
(let [out (with-out-str
|
||||
(println "email console dump:")
|
||||
(println "******** start email" (:id email) "**********")
|
||||
(println (.toString baos))
|
||||
(println "******** end email "(:id email) "**********"))]
|
||||
(log/info out))))
|
||||
|
||||
(defmethod sendmail "console"
|
||||
[config email]
|
||||
(let [out (with-out-str
|
||||
(println "email console dump:")
|
||||
(println "******** start email" (:id email) "**********")
|
||||
(println " from: " (:from email))
|
||||
(println " to: " (:to email "---"))
|
||||
(println " reply-to: " (:reply-to email))
|
||||
(println " subject: " (:subject email))
|
||||
(println " content:")
|
||||
(doseq [item (:content email)]
|
||||
(when (= (:type item) "text/plain")
|
||||
(println (:value item))))
|
||||
(println "******** end email "(:id email) "**********"))]
|
||||
(log/info out)))
|
||||
|
||||
(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)]
|
||||
|
||||
|
||||
(try
|
||||
(let [response (http/send! {:method :post
|
||||
:headers headers
|
||||
:uri "https://api.sendgrid.com/v3/mail/send"
|
||||
:body body})]
|
||||
(when-not (= 202 (:status response))
|
||||
(log/error "Unexpected status from sendgrid:" (pr-str response))))
|
||||
(catch Throwable error
|
||||
(log/error "Error on sending email to sendgrid:" (pr-str error))))))
|
||||
|
||||
(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]
|
||||
(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 adapt-config
|
||||
[cfg]
|
||||
{:host (:smtp-host cfg "localhost")
|
||||
:port (:smtp-port cfg 25)
|
||||
:default-reply-to (:smtp-default-reply-to cfg)
|
||||
:default-from (:smtp-default-from cfg)
|
||||
:tls (:smtp-tls cfg)
|
||||
:username (:smtp-username cfg)
|
||||
:password (:smtp-password cfg)})
|
||||
|
||||
(defn handler
|
||||
{:app.tasks/name "sendmail"}
|
||||
[{:keys [props] :as task}]
|
||||
(sendmail cfg/config props))
|
||||
(if (:smtp-enable cfg/config)
|
||||
(-> (cfg/smtp cfg/config)
|
||||
(emails/send! props))
|
||||
(send-console! props)))
|
||||
|
||||
(mtx/instrument-with-summary!
|
||||
{:var #'handler
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue