-
-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Postal appender: support arbitrary :subject-fn, make default fn smarter
- Loading branch information
1 parent
d9d3342
commit f4c721f
Showing
1 changed file
with
36 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,17 +8,48 @@ | |
[io.aviso.exception :as aviso-ex] | ||
[postal.core :as postal])) | ||
|
||
(defn default-subject-fn | ||
"Given an `output-str`, returns an appropriate email subject string: | ||
- Take only the first line | ||
- Trim it | ||
- Simplify whitespace | ||
- Never exceed `max-subject-len` characters." | ||
|
||
[{:keys [max-len] | ||
:or {max-len 150}} | ||
output-str] | ||
|
||
(let [s (-> | ||
(re-find #"\A.*" output-str) ; 1st line | ||
(str/trim) | ||
(str/replace #"\s+" " "))] | ||
|
||
(if (and max-len (> (count s) ^long max-len)) | ||
(str (enc/get-substring s 0 (- ^long max-len 3)) "...") | ||
s))) | ||
|
||
(comment | ||
(default-subject-fn {:max-len 8} "sdfghsjhfdg shj | ||
sfjsdgfjhsdgf s | ||
sfsdf | ||
sfsdf | ||
sdf")) | ||
|
||
(defn postal-appender | ||
"Returns a Postal email appender. | ||
(postal-appender | ||
^{:host \"mail.isp.net\" :user \"jsmith\" :pass \"sekrat!!1\"} | ||
{:from \"Bob's logger <[email protected]>\" :to \"[email protected]\"})" | ||
|
||
[postal-config & | ||
[{:keys [subject-len body-fn] | ||
[{:keys [subject-len subject-fn body-fn] | ||
:or {subject-len 150 | ||
body-fn (fn [output-str] [{:type "text/plain; charset=utf-8" | ||
:content output-str}])}}]] | ||
subject-fn (partial default-subject-fn {:max-len subject-len}) | ||
body-fn | ||
(fn [output-str] | ||
[{:type "text/plain; charset=utf-8" | ||
:content output-str}])}}]] | ||
|
||
{:enabled? true | ||
:async? true ; Slow! | ||
:min-level :warn ; Elevated | ||
|
@@ -33,11 +64,8 @@ | |
|
||
(postal/send-message | ||
(assoc postal-config | ||
:subject (-> output-str | ||
(str/trim) | ||
(str/replace #"\s+" " ") | ||
(enc/get-substring 0 subject-len)) | ||
:body (body-fn output-str)))))}) | ||
:subject (subject-fn output-str) | ||
:body (body-fn output-str)))))}) | ||
|
||
;;;; Deprecated | ||
|
||
|