Skip to main content

README.md

# Sendr

[![Package Version](https://img.shields.io/hexpm/v/sendr)](https://hex.pm/packages/sendr)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/sendr/)

A Gleam email library to unify sending emails.

## Installation

```sh
gleam add sendr
```

## Usage

```gleam
import sendr
import sendr/message
import sendr/message/mailbox
import sendr/message/attachment
import sendr/backend

// Build a message
let msg =
  message.new()
  |> message.set_from(mailbox.new("Sender", "sender@example.com"))
  |> message.set_to([mailbox.new("Recipient", "recipient@example.com")])
  |> message.set_subject("Hello")
  |> message.set_body_text("Hello, World!")

// Send the message
let assert Ok(req) = backend.request(msg, backend.config())

// Make the HTTP request with your HTTP client of choice:
// let resp = http_client.send(req)
// let result = backend.response(resp)
```

## Backends

Sendr uses a backend architecture so you can use different mail providers. Available backends:

- [sendr_lettermint](https://tangled.org/wommm.nl/sendr_lettermint) - Send emails using [lettermint](https://lettermint.co).
- [sendr_scaleway](https://tangled.org/wommm.nl/sendr_scaleway) - Send emails using [Scaleway](https://www.scaleway.com).
- [sendr_sweego](https://tangled.org/wommm.nl/sendr_sweego) - Send emails using [sweego](https://www.sweego.io).

- [sendr_smtp](https://tangled.org/wommm.nl/sendr_smtp) - Send emails via SMTP.

### Creating your own backend

A backend is expected to have a `request`-function which takes a `sendr/message.Message` and transforms this to a `http.Request`, which can be passed to the backend using any HTTP-client.

In addition the backend should provide a `response`-function which takes in the returned `http.Response` and transforms it to a `Result(String, SendrError(backend_error))` for further processing.

For examples look at the backends listed above.

## Concepts

### Message

A message contains all the standard email fields:

- `from` - The sender
- `reply_to` - Where replies should be sent
- `to` - Primary recipients
- `cc` - Carbon copy recipients
- `bcc` - Blind carbon copy recipients
- `subject` - The email subject line
- `body` - The message body (plain text and/or HTML)
- `attachments` - Files to attach

### Mailbox

A mailbox represents a recipient with a display name and email address. Both are trimmed of whitespace.

### Attachment

Attachments can be created from files or raw data. The content type is automatically detected from the filename extension.

## Development

```sh
gleam test                                # Run the tests
gleam run -m devutils/glinter -- --stats  # Run the code style checks
```