-
Notifications
You must be signed in to change notification settings - Fork 86
/
sendmail.clj
80 lines (73 loc) · 3.19 KB
/
sendmail.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
;; Copyright (c) Andrew A. Raines
;;
;; Permission is hereby granted, free of charge, to any person
;; obtaining a copy of this software and associated documentation
;; files (the "Software"), to deal in the Software without
;; restriction, including without limitation the rights to use,
;; copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the
;; Software is furnished to do so, subject to the following
;; conditions:
;;
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
;; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
;; OTHER DEALINGS IN THE SOFTWARE.
(ns postal.sendmail
(:use [postal.message :only [message->str sender recipients]]))
(def sendmails ["/usr/lib/sendmail"
"/usr/sbin/sendmail"
"/usr/bin/sendmail"
"/usr/local/lib/sendmail"
"/usr/local/sbin/sendmail"
"/usr/local/bin/sendmail"
"/usr/sbin/msmtp"])
(def errors {0 [:SUCCESS "message sent"]
64 [:EX_USAGE "command line usage error"]
65 [:EX_DATAERR "data format error"]
66 [:EX_NOINPUT "cannot open input"]
67 [:EX_NOUSER "addressee unknown"]
68 [:EX_NOHOST "host name unknown"]
69 [:EX_UNAVAILABLE "service unavailable"]
70 [:EX_SOFTWARE "internal software error"]
71 [:EX_OSERR "system error (no fork?)"]
72 [:EX_OSFILE "critical OS file missing"]
73 [:EX_CANTCREAT "can't create (user) output file"]
74 [:EX_IOERR "input/output error"]
75 [:EX_TEMPFAIL "temp failure; user is invited to retry"]
76 [:EX_PROTOCOL "remote error in protocol"]
77 [:EX_NOPERM "permission denied"]
78 [:EX_CONFIG "configuration error"]})
(defn error [code]
(let [[e message] (errors code)]
{:code code
:error e
:message message}))
(defn find-sendmail []
(if-let [SENDMAIL (System/getenv "SENDMAIL")]
SENDMAIL
(first (filter #(.isFile (java.io.File. ^String %)) sendmails))))
(defn sanitize [^String text]
(.replaceAll text "\r\n" (System/getProperty "line.separator")))
(defn sendmail-send
([msg]
(sendmail-send msg (find-sendmail)))
([msg sendmail]
(let [mail (sanitize (message->str msg))
cmd (concat
[sendmail "-f" (sender msg)]
(recipients msg))
pb (ProcessBuilder. ^java.util.List cmd)
p (.start pb)
smtp (java.io.PrintStream. (.getOutputStream p))]
(.print smtp mail)
(.close smtp)
(.waitFor p)
(error (.exitValue p)))))