Add support for full email addresses.

This commit is contained in:
Dominik Pantůček 2024-07-02 20:50:43 +02:00
parent 8c436f6910
commit 7d1101657f

View file

@ -39,6 +39,7 @@
(chicken base)
(chicken keyword)
(chicken string)
(chicken irregex)
util-io
util-utf8
util-string
@ -61,6 +62,14 @@ sent to the address stored within.")
"?=")
subj))
;; Extracts only usernam@domain from given full RFC email address
(define (extract-email-email str)
(let* ((irr (irregex "(?:\"?([^\"]*)\"?\\s)?(?:<?(.+@[^>]+)>?)"))
(em (irregex-match irr str))
(name (irregex-match-substring em 1))
(email (irregex-match-substring em 2)))
email))
;; Sends an email using the UNIX mail(1) utility.
(define*/doc (send-mail body-lines
#:from (from #f)
@ -83,17 +92,22 @@ Sends email using mail(1) command. The arguments ```#:to``` and
tos))
(header-args
(flatten
(append
(if from (list (sprintf "From: ~A" from)) '())
(map
(lambda (h) (list "-a" h))
headers))))
headers)))))
(let ((from-email (if from
(extract-email-email from)
#f)))
(apply process-send/recv
"mail"
(append (if from
(list "-r" from)
(list "-r" from-email)
'())
(list "-s" (encode-subject subject))
real-tos
header-args)
body-lines)))
body-lines))))
)