net/sendmail: delay executable search until needed

This commit is contained in:
Matthew Flatt 2015-10-19 18:57:02 -06:00
parent f378aa1b7a
commit ba661e009b

View File

@ -1,4 +1,5 @@
#lang racket/base #lang racket/base
(require racket/promise)
(provide send-mail-message/port send-mail-message) (provide send-mail-message/port send-mail-message)
@ -8,18 +9,19 @@
'("/usr/sbin" "/sbin" "/usr/local/sbin" "/usr/lib")) '("/usr/sbin" "/sbin" "/usr/local/sbin" "/usr/lib"))
(define sendmail-program-file (define sendmail-program-file
(let ([exe (case (system-type) (delay
[(windows) "sendmail.exe"] (let ([exe (case (system-type)
[else "sendmail"])]) [(windows) "sendmail.exe"]
(or (for/or ([path (in-list sendmail-search-path)]) [else "sendmail"])])
(define p (build-path path exe)) (or (for/or ([path (in-list sendmail-search-path)])
(and (file-exists? p) (define p (build-path path exe))
(memq 'execute (file-or-directory-permissions p)) (and (file-exists? p)
p)) (memq 'execute (file-or-directory-permissions p))
(raise (make-exn:fail:unsupported p))
(format "unable to find a sendmail executable in ~s" (raise (make-exn:fail:unsupported
sendmail-search-path) (format "unable to find a sendmail executable in ~s"
(current-continuation-marks)))))) sendmail-search-path)
(current-continuation-marks)))))))
;; Main implementation, returns a port ;; Main implementation, returns a port
(define (send-mail-core who sender subject TOs CCs BCCs headers) (define (send-mail-core who sender subject TOs CCs BCCs headers)
@ -31,7 +33,7 @@
(error who "no mail recipients were specified")) (error who "no mail recipients were specified"))
(define-values [p pout pin perr] (define-values [p pout pin perr]
;; use -i, so "." lines are not a problem ;; use -i, so "." lines are not a problem
(apply subprocess #f #f #f sendmail-program-file "-i" all-recipients)) (apply subprocess #f #f #f (force sendmail-program-file) "-i" all-recipients))
(close-input-port pout) (close-input-port pout)
(close-input-port perr) (close-input-port perr)
(port-count-lines! pin) (port-count-lines! pin)