Skip to content

Commit

Permalink
Add denylist for sending emails
Browse files Browse the repository at this point in the history
This allows us to ignore a testing account with an invalid email
address.
  • Loading branch information
tobias committed Aug 12, 2023
1 parent 3328761 commit 0d33a46
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
6 changes: 6 additions & 0 deletions resources/email-denylist.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
;; A set of addresses that we never want to send an email to.
#{
;; Used by GitHub on the account they use for integration testing token breach
;; reporting, but it doesn't actually exist, resulting in monitoring noise
"[email protected]"
}
39 changes: 23 additions & 16 deletions src/clojars/email.clj
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
(ns clojars.email
(:require
[clojars.log :as log])
[clojars.log :as log]
[clojure.edn :as edn]
[clojure.java.io :as io])
(:import
(java.util.concurrent
CountDownLatch
TimeUnit)
(org.apache.commons.mail
SimpleEmail)))

(def ^:private email-denylist
(edn/read-string (slurp (io/resource "email-denylist.edn"))))

(defn simple-mailer [{:keys [hostname username password port tls? from]}]
(fn [to subject message]
(log/with-context {:tag :email
:email-to to
:email-subject subject}
(try
(let [mail (doto (SimpleEmail.)
(.setHostName (or hostname "localhost"))
(.setSmtpPort (or port 25))
(.setStartTLSEnabled (boolean tls?))
(.setStartTLSRequired (boolean tls?))
(.setFrom (or from "[email protected]") "Clojars")
(.addTo to)
(.setSubject subject)
(.setMsg message))]
(when tls?
(.setSslSmtpPort mail (str (or port 25))))
(when (and username password)
(.setAuthentication mail username password))
(.send mail)
(log/info {:status :success}))
(if (contains? email-denylist to)
(log/info {:status :denylist})
(let [mail (doto (SimpleEmail.)
(.setHostName (or hostname "localhost"))
(.setSmtpPort (or port 25))
(.setStartTLSEnabled (boolean tls?))
(.setStartTLSRequired (boolean tls?))
(.setFrom (or from "[email protected]") "Clojars")
(.addTo to)
(.setSubject subject)
(.setMsg message))]
(when tls?
(.setSslSmtpPort mail (str (or port 25))))
(when (and username password)
(.setAuthentication mail username password))
(.send mail)
(log/info {:status :success})))
(catch Exception e
(log/error {:status :failed
:error e})
Expand Down

0 comments on commit 0d33a46

Please sign in to comment.