Sending an email via SMTP

I know of 2 that look more or less decent:

What library would you recommend?
It should be more or less mature, and not abandoned.

There is mrmime for building the email and colombe for the SMTP protocol.
Letters is a simple (easier to use but limited) wrapper over these libraries.

Here is an example of use of mrmime: https://github.com/Julow/rss_to_mail/blob/master/main_native/mail.ml#L33-L106
and colombe: https://github.com/Julow/rss_to_mail/blob/master/main_native/mail.ml#L110-L132

Other examples I know are unfortunately outdated.

1 Like

Like @Juloo said, colombe and mrmime are there for sending emails over SMTP. While very powerful and somewhat low level, they are not too easy to use. That’s why I created letters that aims to be super easy to use library, while somewhat limited in features. It is still build on mrmime and colombe so you can also use it as an example.

I’m in the process of adding mimetype/alternative support in few days and there’s also PR in works for easier discovery for CA certificates.

After that I think we’ll tackle attachments and better message abstraction (sihl needs support for serialising messages for job queues).

If you prefer generating your emails manually, you can still generate them using mrmime and use letters to simplify sending them. Later I’ll add also support to use any other string stream to provide the email itself, if you don’t want to use mrmime.

Feel free to drop any improvement ideas or feature requests and we’ll see when those can be implemented. Currently the development has been a bit slow as I’ve been otherwise busy, but hopefully we can speed up things soon.

2 Likes

I have found a limitation of letters: This library does not support SMTP connections without TLS encryption.

Are there simple alternatives with plain (unencrypted) SMTP (I guess nobody will spoof the locahost traffic) ? Or Is it simpler to configure postfix to support STARTTLS

You can use libcurl via the ocurl bindings (see libcurl example - smtp-mail.c for an example in C of how to send an email).

Cheers,
Nicolas

I usually find it simpler to install and configure msmtp. On debian if you install msmtp-mta aswell it also symlinks sendmail to it, so it’s easy to send email from scripts by just printfing or OCaml by shelling out (warning the code sample is a bit naive it’s likely missing a bit of escaping).