package letters

  1. Overview
  2. Docs
Client library for sending emails over SMTP

Install

Dune Dependency

Authors

Maintainers

Sources

letters-0.2.1.tbz
sha256=ed825c2da0d127c5edb64179e5858f2535b50e20d99c516b3d044e600676cbd5
sha512=16fd185ba0e06d8200e3fbdc7abae2a76b96e9d97b0750904f7200b3963dc9b45838576984bc74dfdc56189667c2c7eb70e9ca0343eeef9f8792d64a484a16de

Description

Simple to use SMTP client implementation for OCaml

Published: 05 Dec 2020

README

✉ Letters ·

Letters is a library for creating and sending emails over SMTP using Lwt.

Table of Contents

Use

Purpose of the library is to make it easier to send emails when building systems using OCaml. Currently the API consists of three parts:

  1. configuration

  2. building email messages

  3. sending email messages

Whole API is in lib/letters.mli that contains also some additional documentation.

Keep in mind that this library is in its early days and the API is changing with every release. Also this is tested only on Linux based systems and testing is pretty weak and manual. Though the library has been used successfully.

Configuration

Most simple use case would look something like:

let conf = Config.make ~username:"myuser" ~password:"mypasswd" ~hostname:"smtp.ethereal.email" ~with_starttls:true

This will use port 587, uses STARTTLS for encryption and tries automatically find CA certificates for verifying server connection.

Port 587 is default when using STARTTLS. If you set ~with_starttls:false, then the default port will be 465.

This library does not support SMTP connections without TLS encryption or authentication. For TLS encryption, this library uses ocaml-tls.

If you want to change the server port you can do it with Config.set_port (passing None causes default port to be used):

let conf = Config.make ~username:"myuser" ~password:"mypasswd" ~hostname:"smtp.ethereal.email" ~with_starttls:true
|> Config.set_port (Some 2525)

If the CA certificate auto-detection does not work for you (it's very naïve implementation), you can define path to a certificate bundle or to a single PEM encoded certificate, or you can define path to a folder containing multiple PEM encoded certificate files.

To use a CA certificate bundle (each included certificate needs to be PEM encoded):

let conf = Config.make ~username:"myuser" ~password:"mypasswd" ~hostname:"smtp.ethereal.email" ~with_starttls:true
|> Config.set_ca_cert "/etc/ssl/certs/ca-certificates.crt"

To use a single PEM encoded CA certificate:

let conf = Config.make ~username:"myuser" ~password:"mypasswd" ~hostname:"smtp.ethereal.email" ~with_starttls:true
|> Config.set_ca_cert "/etc/ssl/certs/DST_Root_CA_X3.pem"

To use all PEM encoded certificate files from a folder:

let conf = Config.make ~username:"myuser" ~password:"mypasswd" ~hostname:"smtp.ethereal.email" ~with_starttls:true
|> Config.set_ca_path "/etc/ssl/certs/"

Building emails

Building an email is separated into its own step so that you can use mrmime to generate more complex emails when this simplified API does not work for you.

To use our provided API, you can build three kinds of emails:

  1. Plain, plain text

  2. Html, HTML only

  3. Mixed, multipart/alternative containing both: plain text and HTML segments

If you're not sure, either use Plain or Mixed.

Example of building a plain text email:

let sender = "harry@example.com" in
let recipients =
  [
    To "larry@example.com";
    Cc "bill@example.com";
    Bcc "dave@example.com";
  ]
in
let subject = "Plain text only test email" in
let body =
  Plain
    {|
Hi there,

This is a test email from https://github.com/oxidizing/letters

Regards,
The Letters team
|}
  in
  let mail = build_email ~from:sender ~recipients ~subject ~body in

Example of building an HTML only email:

let sender = "harry@example.com" in
let recipients =
  [
    To "larry@example.com";
    Cc "bill@example.com";
    Bcc "dave@example.com";
  ]
in
let subject = "HTML only test email" in
let body =
  Html
    {|
<p>Hi there,</p>
<p>
    This is a test email from
    <a href="https://github.com/oxidizing/letters">letters</a>
<p>
Regards,<br>
The Letters team
</p>
|}
in
let mail = build_email ~from:sender ~recipients ~subject ~body in

Example of building an email with plain text and HTML segments:

let sender = "harry@example.com" in
let recipients =
  [
    To "larry@example.com";
    Cc "bill@example.com";
    Bcc "dave@example.com";
  ]
in
let subject = "Mixed plain text / HTML test email" in
let text =
  {|
Hi there,

This is a test email from https://github.com/oxidizing/letters

Regards,
The Letters team
|}
in
let html =
  {|
<p>Hi there,</p>
<p>
    This is a test email from
    <a href="https://github.com/oxidizing/letters">letters</a>
<p>
Regards,<br>
The Letters team
|}
in
let mail = build_email ~from:sender ~recipients ~subject ~body:(Mixed (text, html, None)) in

Letters.build_email returns result so you need to map it accordingly:

let mail = build_email ~from:sender ~recipients ~subject ~body:(Mixed (text, html, None)) in
match mail with
| Ok message -> do_something message
| Error reason -> handle_error reason

Sending emails

Sending is single API call Letters.send that looks like following (when using config, sender, recipients and message from previous examples):

send ~config ~sender ~recipients ~message

Return type is Lwt.t so you need to run it with appropriate Lwt routines.

Examples

See service-test/test.ml for complete examples that are using ethereal.email service to test sending emails.

Development

Setup

opam switch create . ocaml-base-compiler.4.08.1
eval $(opam env)
opam install --deps-only -y . --with-test

Build

dune build

Tests

Unit tests

Run with default test target of dune:

dune build @runtest

These tests are still somewhat far from good and you need to validate all results manually by checking the test output logs.

Service tests

Because these tests are somewhat slow and fragile, you need to run them manually. Execution of these tests depends on test accounts on ethereal.email and mailtrap.io. Before execution, you need to create configuration files with authentication credentials for each service. You can generate these configuration files by using the shell command snippets given below, but for those you need to have jq application installed. If you don't have it or you don't want to install it, you can also create ethereal_account.json and mailtrap_account.json files manually. Both files have the following format:

{
  "host": "smtp.ethereal.email or smtp.mailtrap.io",
  "port": 587,
  "username": "username for SMTP authentication",
  "password": "password for SMTP authentication",
  "secure": false // we will always use encryption, but `false` causes use of STARTTLS
}

To create temporary ethereal.email account and store the account details, you can execute the following one-liner:

curl -s -d '{ "requestor": "letters", "version": "dev" }' "https://api.nodemailer.com/user" -X POST -H "Content-Type: application/json" | jq '{ hostname: .smtp.host, port: .smtp.port, secure: false, username: .user, password: .pass, }'> ethereal_account.json

For mailtrap.io, you need to create a personal account first and get the API key:

The configuration file you can create with following steps:

  • create environment variable containing your API token: export MAILTRAP_API_TOKEN=<API token>

  • run the following one-liner in terminal to create the configuration file:

curl -s -H "Authorization: Bearer ${MAILTRAP_API_TOKEN}" "https://mailtrap.io/api/v1/inboxes" | jq '.[0] | { hostname: .domain, port: .smtp_ports[2], secure: false, username: .username, password: .password }' > mailtrap_account.json

Now you are ready to execute these tests. You can run them with the following command:

dune build @runtest-all

Finally review that all emails are correctly received in ethreal.email:

  • login to https://ethereal.email/login using credentials from the ethereal_account.json

  • check the content of new messages: https://ethereal.email/messages

Also check that you can find all emails in the inbox in mailtrap.io:

  • login to mailtrap.io using your personal credentials

  • select the first inbox (unless you use another one), from inboxes

  • check the content of new messages

Credits

This project is build on colombe and mrmime libraries and use facteur as starting point.

License

Copyright (c) 2020 Miko Nieminen

Distributed under the MIT License.

Dependencies (11)

  1. tls < "0.16.0"
  2. fpath >= "0.7.0"
  3. lwt >= "5.2.0"
  4. ptime >= "0.8.5"
  5. x509 >= "0.10.0"
  6. fmt >= "0.8.8"
  7. sendmail >= "0.4.0"
  8. colombe >= "0.4.0" & < "0.5.0"
  9. mrmime >= "0.3.1" & < "0.6.0"
  10. dune >= "2.3"
  11. ocaml >= "4.08.1"

Dev Dependencies (5)

  1. ocamlformat dev
  2. odoc with-doc
  3. yojson >= "1.7.0" & with-test
  4. alcotest-lwt >= "1.1.0" & with-test
  5. alcotest >= "1.1.0" & with-test

Used by (2)

  1. sihl >= "0.1.0" & < "0.1.5"
  2. sihl-email

Conflicts

None