send-email-msmtp.scm 1023 B

123456789101112131415161718192021222324252627282930
  1. (define-module (send-email-msmtp)
  2. #:use-module (ice-9 popen)
  3. #:use-module (srfi srfi-19)
  4. #:export (send-email))
  5. ;; see the last line from this page:
  6. ;; https://www.gnu.org/software/guile/manual/html_node/Random.html
  7. (set! *random-state* (random-state-from-platform))
  8. (define (send-email to subject body)
  9. (let ((port (open-pipe* OPEN_WRITE "msmtp" to)))
  10. (display (email-rfc-2822 subject body) port)
  11. (close-pipe port)))
  12. (define (email-rfc-2822 subject body)
  13. (string-append
  14. ;;"From: joshua@gnucode.me"
  15. "Subject: " subject "\n"
  16. ;; email date format: https://datatracker.ietf.org/doc/html/rfc5322#section-3.3
  17. (string-append "Date: " (date->string (current-date) "~a, ~e ~b ~Y ~T ~z") "\n")
  18. ;;
  19. (string-append "Message-ID: <"
  20. (number->string (random 1000000 *random-state*))
  21. "@local.machine.example>\n\n")
  22. body))
  23. (send-email "jbranso@dismail.de"
  24. "I hopefully have dkim?"
  25. "This is another test email for joshua@gnucode.me")