Elixir
Use the official Lettermint Elixir SDK to send email from an Elixir application. The SDK also supports the Team API and webhook signature verification. See SDKs and integrations for other packages.
Requirements
Before you start, you need:
- Elixir 1.15 or newer with a compatible Erlang/OTP release
- A Mix project
- A Lettermint account with a verified sending domain
- A Project API token from your project settings
1. Installation
Add the Lettermint Hex package to the existing dependency list in mix.exs:
Code
Install the dependency:
Code
The SDK uses Req for HTTP requests and Jason for JSON. Mix installs these dependencies. No application configuration is required.
2. Send your first email
Set LETTERMINT_PROJECT_TOKEN in your application's environment. Use a Project API token for email sends.
Create a script in your project directory:
Code
Replace the sender with an address on your verified domain. Replace the recipient with your test address, then run the script:
Code
API calls return {:ok, result} or {:error, %Lettermint.Error{}}. The first example matches a successful response. Use a case expression to handle failures, as shown in the error section below.
3. Email options
The examples below use the client from the previous section. Request maps accept atom or string keys.
Recipients, reply-to, and headers
Code
The payload's headers field sets email headers. Use the idempotency_key request option for the HTTP idempotency header.
Tags and metadata
Code
Use tags for named tags or tag: "order-confirmation" for a single tag. Use string values for metadata. Lettermint includes metadata in webhook payloads so you can connect delivery events to application records. See tags for tag options.
Route and delivery settings
Code
route selects a route by its slug. The tracking settings control open and click tracking for this email. tls: "enforced" requires TLS for email delivery. See tracking and TLS.
Idempotency
Pass an idempotency key as a request option:
Code
Use the same key when you retry the same email. The SDK does not retry requests automatically. See idempotency for the API rules and retention window.
File attachments
Read the file and encode its content as Base64:
Code
For an inline image, set content_id on the attachment and use the matching cid: reference in the HTML body. See inline images.
Scheduled emails
Use a future timestamp with an explicit time zone:
Code
The SDK also provides Lettermint.Messages.reschedule/3 and Lettermint.Messages.cancel/2. See email scheduling for scheduling limits and cancellation behavior.
4. Use the pipe operator
Use Lettermint.EmailBuilder to build an email in separate steps:
Code
Each builder function replaces one field and returns a new builder. Use Email.to_map/1 to prepare an item for Lettermint.Email.send_batch/2.
5. Responses and errors
Successful JSON responses use generated structs. For an email send, read message_id and status. API acceptance does not confirm delivery. Use webhooks for delivery and bounce events.
Handle API errors and other SDK failures with a case expression:
Code
Error kinds are :api, :transport, and :decode. API errors include the HTTP status and a body field. The SDK removes token text from error data. Invalid local arguments raise ArgumentError.
To set the request timeout, pass milliseconds when you create the client:
Code
The timeout applies separately to connection setup and response receipt.
Team API
Use a separate Team API token to manage team resources. Set it in LETTERMINT_TEAM_TOKEN:
Code
Do not use a Team API token with the email client. See the Team API quickstart for token setup and permissions.
Track delivery, opens, and bounces
Enable tracking on your route to record opens and clicks. Subscribe to webhooks for delivery and bounce events.
Use Lettermint.Webhook.verify(raw_body, signature_header, webhook_secret) to verify webhook signatures. Pass the original request body before JSON decoding. Verification returns :ok or {:error, :invalid_signature}. See webhook signing for the header and signing secret.
Use Lettermint with Swoosh
If your application uses Swoosh, use its Lettermint adapter for mail delivery. The official SDK provides direct Sending API and Team API access. The Swoosh adapter remains a separate integration.
Next steps
Tags
Organize emails with tags.
Webhooks
Receive delivery and bounce events.
Test addresses
Test delivery and bounce handling.
Hex package
Find package releases and dependencies.
GitHub repository
Read the source code or report an issue.