Using SparkPost with Elixir

June 21, 2017 Contributors

This article assumes you are familiar with Elixir and the mix and iex CLIs. We also assume that you have created an API key with the “Send via SMTP” permission.

This article will show you how to use SparkPost via SMTP with Elixir and the Mailman library.

Add the dependencies to your mix.exs file:

Install the dependencies:

Next up, create an Elixir module that uses Mailman, an abstraction on top of Erlang’s gen_smtp library. With Mailman you create a context and an email. Think of the context as the configuration you use to connect to SparkPost, and the email as the message you are going to send. You then invoke the Mailman.deliver method and pass it both the email and the context.

Create a new file called mailer.ex with the following contents and save it in lib/myapp/mailer.ex, replacing the values with your information:

In your terminal, test out the deliver method using iex:

You are now ready to start sending email through SparkPost using Elixir. Modify the deliver method above as you’d like and call it from wherever you’d like in your application. The Mailman.deliver method returns either an error tuple or an ok tuple. Check out Mailman’s source code on github for more info.