hackerbase/src/util-mail.scm

91 lines
2.3 KiB
Scheme

;;
;; util-mail.scm
;;
;; Helper for sending emails.
;;
;; ISC License
;;
;; Copyright 2023 Brmlab, z.s.
;; Dominik Pantůček <dominik.pantucek@trustica.cz>
;;
;; Permission to use, copy, modify, and/or distribute this software
;; for any purpose with or without fee is hereby granted, provided
;; that the above copyright notice and this permission notice appear
;; in all copies.
;;
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
;; WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
;; WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
;; AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
;; CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
;; OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
;; NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
;; CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
;;
(declare (unit util-mail))
(import duck)
(module*
util-mail
#:doc ("A simple wrapper module to send emails from UNIX system.")
(
*mailto-override*
send-mail
)
(import scheme
(chicken base)
(chicken keyword)
(chicken string)
util-io
util-utf8
util-string
racket-kwargs)
;; All emails go to this override
(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)
(if (string-utf8? subj)
(string-append "=?UTF-8?Q?"
(string->qp subj)
"?=")
subj))
;; Sends an email using the UNIX mail(1) utility.
(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)))
)