Duck util-mail.

This commit is contained in:
Dominik Pantůček 2023-07-06 20:06:15 +02:00
parent 23db356dec
commit b9a563d4ac
5 changed files with 73 additions and 49 deletions

View file

@ -25,8 +25,11 @@
(declare (unit util-mail))
(module
(import duck)
(module*
util-mail
#:doc ("A simple wrapper module to send emails from UNIX system.")
(
*mailto-override*
send-mail
@ -38,10 +41,17 @@
(chicken string)
util-io
util-utf8
util-string)
util-string
racket-kwargs)
;; All emails go to this override
(define *mailto-override* (make-parameter #f))
(define/doc *mailto-override*
("* ```email``` - email address string
If this parameter is non-```#f```, all outgoing emails are actually
sent to the address stored within.")
email
(make-parameter #f))
;; Ensures the subject has proper encoding
(define (encode-subject subj)
@ -52,27 +62,30 @@
subj))
;; Sends an email using the UNIX mail(1) utility.
(define (send-mail body-lines . args)
(let ((from (get-keyword #:from args))
(to (get-keyword #:to args))
(subject (get-keyword #:subject args)))
(when (not to)
(error 'send-mail "requires #:to argument"))
(when (not subject)
(error 'send-mail "requires #:subject argument"))
(let* ((tos (if (list? to)
to
(list to)))
(real-tos (if (*mailto-override*)
(list (*mailto-override*))
tos)))
(apply process-send/recv
"mail"
(append (if from
(list "-r" from)
'())
(list "-s" (encode-subject subject))
real-tos)
body-lines))))
(define*/doc (send-mail body-lines
#:from (from #f)
#:to to
#:subject subject)
("* ```body-lines``` - lines of the email
* ```from``` - email address from string
* ```to``` - email address to string
* ```subject``` - email subject string
Sends email using mail(1) command. The arguments ```#:to``` and
```#:subject``` are mandatory. Argument ```#:from``` is optional.")
(let* ((tos (if (list? to)
to
(list to)))
(real-tos (if (*mailto-override*)
(list (*mailto-override*))
tos)))
(apply process-send/recv
"mail"
(append (if from
(list "-r" from)
'())
(list "-s" (encode-subject subject))
real-tos)
body-lines)))
)