Skip to content

toyokumo/tarayo

Repository files navigation

tarayo

SMTP client library for Clojure. That’s it.

Why tarayo?

Tarayo is heavily inspired by drewr/postal.

  • Only targets SMTP

  • Explicit connection

    • Handle the connection manually.

  • Well tested

"Tarayo" is a tree name called "Tree of post office" in Japan.

Usage

tarayo

(require '[tarayo.core :as tarayo])
;; => nil

Connection SMTP server

tarayo.core/connect is a function to connect SMTP server.
You need to call tarayo.core/close function before quitting, or use with-open macro.

(type (tarayo/connect {:host "localhost" :port 25}))
;; => tarayo.core.SMTPConnection

Other examples are follows:

SSL connection

(tarayo/connect {:host "localhost" :port 465 :ssl.enable true})

TLS connection

(tarayo/connect {:host "localhost" :port 587 :starttls.enable true})

Connection with user authentication

(tarayo/connect {:host "localhost" :port 25 :user "USERNAME" :password "PASSWORD"})

Sending mails

Text mail

(with-open [conn (tarayo/connect {:host "localhost" :port 25})]
  (tarayo/send! conn {:from "alice@example.com"
                      :to "bob@example.com"
                      :subject "hello"
                      :body "world"}))
;; => {:result :success, :code 250, :message "250 OK\n"}

HTML mail

(with-open [conn (tarayo/connect {:host "localhost" :port 25})]
  (tarayo/send! conn {:from "alice@example.com"
                      :to "bob@example.com"
                      :subject "hello"
                      :content-type "text/html"
                      :body "<h1>world</h1>"}))
;; => {:result :success, :code 250, :message "250 OK\n"}

Reply to

(with-open [conn (tarayo/connect {:host "localhost" :port 25})]
  (tarayo/send! conn {:from "alice@example.com"
                      :to "bob@example.com"
                      :reply-to "charlie@example.com"
                      :subject "hello"
                      :body "world"}))
;; => {:result :success, :code 250, :message "250 OK\n"}

Attachment file

(require '[clojure.java.io :as io])
;; => nil

(with-open [conn (tarayo/connect {:host "localhost" :port 25})]
  (tarayo/send! conn {:from "alice@example.com"
                      :to "bob@example.com"
                      :subject "hello"
                      ;; Default multipart type is "mixed"
                      :body [;; string content will be handled as "text message" while others are handled as "attachment file"
                             {:content "world"}
                             ;; If you don't specify `:content-type`, tarayo will detect it using Apache Tika automatically.
                             {:content (io/file "test/resources/file")}
                             ;; Of cource, you can specify `:content-type` manually.
                             {:content (io/file "test/resources/image.png") :content-type "image/png"}
                             ;; You could also use byte array for `:content`.
                             {:content (java.nio.file.Files/readAllBytes (.toPath (io/file "test/resources/image.png")))
                              ;; In this case, `:content-type` and `:filename` must be specified.
                              :content-type "image/png" :filename "new.png"}]}))
;; => {:result :success, :code 250, :message "250 OK\n"}

Multipart/alternative

(with-open [conn (tarayo/connect {:host "localhost" :port 25})]
  (tarayo/send! conn {:from "alice@example.com"
                      :to "bob@example.com"
                      :subject "hello"
                      :multipart "alternative"
                      :body [{:content-type "text/plain" :content "world"}
                             {:content-type "text/html" :content "<h1>wold</h1>"}]}))
;; => {:result :success, :code 250, :message "250 OK\n"}

Inline image (Multipart/related)

(require '[clojure.java.io :as io]
         '[tarayo.mail.mime.id :as mime-id])
;; => nil

(with-open [conn (tarayo/connect {:host "localhost" :port 25})]
  (let [content-id (mime-id/get-random)]
    (tarayo/send! conn {:from "alice@example.com"
                        :to "bob@example.com"
                        :subject "hello"
                        :multipart "related"
                        :body [{:content (str "<img src=\"cid:" content-id "\" /> world") :content-type "text/html"}
                               ;; containing id will be handled as "inline attachment file"
                               {:content (io/file "test/resources/image.png") :id content-id}]})))
;; => {:result :success, :code 250, :message "250 OK\n"}

Use for Gmail API

Like above, tarayo only supports SMTP, but you can also use for generating parameter to call Gmail API.

The entire email message in an RFC 2822 formatted and base64url encoded string. Returned in messages.get and drafts.get responses when the format=RAW parameter is supplied.

To generate this parameter, you can use tarayo.mail.mime.

(require '[tarayo.mail.mime :as mime]
         '[tarayo.mail.session :as session])
;; => nil

(defn- mime-message->raw-string [^jakarta.mail.internet.MimeMessage mime-msg]
  (let [buf (java.io.ByteArrayOutputStream.)]
    (.writeTo mime-msg buf)
    (org.apache.commons.codec.binary.Base64/encodeBase64URLSafeString (.toByteArray buf))))
;; => any?

(let [msg {:from "alice@example.com"
           :to "bob@example.com"
           :subject "hello"
           :body "world"}
      mime-msg (mime/make-message (session/make-session) msg)]
  (mime-message->raw-string mime-msg))
;; => string?

Stubbing

Example using shrubbery.

(require '[shrubbery.core :as shrubbery])
;; => nil

(let [conn (shrubbery/stub
            tarayo/ISMTPConnection
            {:send! "ok"
             :connected? true
             :close true})]
  (tarayo/send! conn "foo"))
;; => "ok"

License

Copyright 2020-2023 Toyokumo,Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.