As mentioned in my other post about plaintext emails, I recommend using the Apache Commons Email library when sending email from Clojure apps. First up, you need to add the dependency to ‘project.clj’
1 2 3 4 |
:dependencies [ [org.clojure/clojure "1.10.1"] [org.apache.commons/commons-email "1.5"] ] |
Next, your HTML email has both an HTML version and the alternative plaintext version of the email. Notice, all you have to do is create an instance of HtmlEmail and then call the set and send methods on it using ‘doto’. Pretty easy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
(ns email-example.core (:import (org.apache.commons.mail HtmlEmail))) (defn -main "I don't do a whole lot ... yet." [& args] (println "Email the World ...") (let [smtp-address "smtp.example.com" subject "Test email" msg-html "<html>This is your basic html text email.<br>Enjoy!</html>" msg-plain "This is your basic alternate plain text email.\nEnjoy!"] (doto (HtmlEmail.) (.setHostName smtp-address) (.setFrom from) (.setSubject subject) (.addTo to) (.setHtmlMsg msg-html) (.setTextMsg msg-plain) (.send))) (println "... Email Sent!")) |
There are more examples and the javadocs over at the Apache Commons site. However, this should be a basic enough example to get you started.