Dynamic Swoosh configs

I'm working on a new project(Keep All The Things) which requires the admin user to configure both sending and receiving email.  For sending email I plan to use Swoosh but wanted a better experience for the admin than setting environmental variables and I wanted a better experience for the dev then a bunch of if statements in the config files to figure out which adapter to use.

Unfortunately I couldn't find any posts or details in the docs about how to configure the Swoosh mailer outside of the config files or the mailer.ex module but luckily I found a issue for Bamboo that explained how to do it in Bamboo and realised it'd work for Swoosh as well.

import Swoosh.Email

mailer_config = [adapter: Swoosh.Adapters.Mailgun,
                 api_key: "api_key",
                  domain: "keepallthethings.com"]
email = new() 
        |> to("shawn@shawnonthe.net")
        |> from("shawn@keepallthethings.com")
        |> subject("Example Message")
        |> text_body("Example message")
        |> html_body("Example message")

Katt.Mailer.deliver(email, mailer_config)

With this style of configuration it makes it very easy to store your mail configuration in the database so your users can edit it from their interface instead of using config files or environment variables.

Subscribe to Shawn on the net

Sign up now to get access to the library of members-only issues.
Jamie Larson