Skip to main content

Elixir MailSlurp Library

Official Elixir library for MailSlurp. Create email accounts then send and receive email from Elixir, Erlang, Phoenix and more.

Install

There are multiple ways to install MailSlurp.

Note: replace the version shown with the latest version on hex.pm.

Mix.exs

{:mailslurp, "~> 11.6"}

Rebar.config

{mailslurp, "11.6.0"}

Erlang.mk

dep_mailslurp = hex 11.6.0

MailSlurp uses Tesla client and httpc by default. Hackney is recommended as it handles some MailSlurp POST methods with empty bodies.

defp deps do
[
{:hackney, "~> 1.17.4"},
{:mailslurp, "~> 11.6.1"}
]
end

Configure client

Get API KEY

Create a MailSlurp account to obtain a free API KEY.

find api key

Configure Tesla

The MailSlurp REST API is split into controllers. Each method accepts a Tesla client as the first parameter.

# create a tesla client for MailSlurp controllers
middleware = [
# set x-api-key header with MailSlurp API Key
{Tesla.Middleware.Headers, [{"x-api-key", "your-mailslurp-api-key-here"}]},
{Tesla.Middleware.BaseUrl, "https://api.mailslurp.com"},
{Tesla.Middleware.EncodeJson, engine: Poison}
]
# use hackney adapter instead of httpc
adapter = {Tesla.Adapter.Hackney, [recv_timeout: 30_000]}
# create a connection
connection = Tesla.client(middleware, adapter)

Use controllers

Controller method return values can be matched against :ok, T or :error, :info.

# handle ok result and response
{ :ok, inbox } = MailSlurpAPI.Api.InboxController.create_inbox(connection)
# handle error
{ :error, info } = MailSlurpAPI.Api.InboxController.create_inbox(connection)

Examples

Here are some common use cases. See the documentation or examples pages for more information.

Create inbox

Inboxes have an emailAddress and id.

# create test email address
{ :ok, inbox } = MailSlurpAPI.Api.InboxController.create_inbox(connection)
assert inbox.emailAddress =~ "@mailslurp."

Receive email

{:ok, email} = MailSlurpAPI.Api.WaitForController.wait_for_latest_email(connection, [
:inboxId = inbox.id,
:timeout = 30_000,
:unreadOnly = true
])

Send email

MailSlurpAPI.Api.InboxController.send_email(connection, inbox.id, [
to: [inbox.emailAddress],
subject: "test",
body: "Hello"
])