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 base)
(chicken keyword) (chicken keyword)
(chicken string) (chicken string)
(chicken irregex)
util-io util-io
util-utf8 util-utf8
util-string util-string
@ -61,6 +62,14 @@ sent to the address stored within.")
"?=") "?=")
subj)) 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. ;; Sends an email using the UNIX mail(1) utility.
(define*/doc (send-mail body-lines (define*/doc (send-mail body-lines
#:from (from #f) #:from (from #f)
@ -83,17 +92,22 @@ Sends email using mail(1) command. The arguments ```#:to``` and
tos)) tos))
(header-args (header-args
(flatten (flatten
(append
(if from (list (sprintf "From: ~A" from)) '())
(map (map
(lambda (h) (list "-a" h)) (lambda (h) (list "-a" h))
headers)))) headers)))))
(let ((from-email (if from
(extract-email-email from)
#f)))
(apply process-send/recv (apply process-send/recv
"mail" "mail"
(append (if from (append (if from
(list "-r" from) (list "-r" from-email)
'()) '())
(list "-s" (encode-subject subject)) (list "-s" (encode-subject subject))
real-tos real-tos
header-args) header-args)
body-lines))) body-lines))))
) )