1
votes

I am developing a backend using Elixir/Phoenix. That backend will be used by several frontends and each one needs to send emails using a different smtp server/configuration.

How can I achieve that with bamboo email?

2
Did you try creating a mailer for each SMTP? As far as I can tell, you just need to create a Mailer module and set its configuration as shown here: github.com/thoughtbot/bamboo#getting-started.Dogbert
@Dogbert the configuration must be setted dinamically on execution time from database recordsMou

2 Answers

1
votes

I have not tested this, but I think it could work:

    # In your config/config.exs file
    #
    # There may be other adapter specific configuration you need to add.
    # Be sure to check the adapter's docs. For example, Mailgun requires a `domain` key.
    config :my_app, MyApp.MandrillMailer,
      adapter: Bamboo.MandrillAdapter,
      api_key: "my_api_key"

    # Configure another adapter
    config :my_app, MyApp.SendGridMailer,
      adapter: Bamboo.SendGridAdapter,
      api_key: "my_api_key"

    # Somewhere in your application
    defmodule MyApp.MandrillMailer do
      use Bamboo.Mailer, otp_app: :my_app
    end

    defmodule MyApp.SendGridMailer do
      use Bamboo.Mailer, otp_app: :my_app
    end

    # Define your emails
    defmodule MyApp.Email do
      import Bamboo.Email

      def welcome_email do
        new_email(
          to: "[email protected]",
          from: "[email protected]",
          subject: "Welcome to the app.",
          html_body: "<strong>Thanks for joining!</strong>",
          text_body: "Thanks for joining!"
        )

        # or pipe using Bamboo.Email functions
        new_email
        |> to("[email protected]")
        |> from("[email protected]")
        |> subject("Welcome!!!")
        |> html_body("<strong>Welcome</strong>")
        |> text_body("welcome")
      end
    end

    # In a controller or some other module
    # Use the MandrilMailer to send this message
    Email.welcome_email |> MandrillMailer.deliver_now

    # You can also deliver emails in the background with Mailer.deliver_later
    # Use the SendGridMailer to send this message
    Email.welcome_email |> SendGridMailer.deliver_later

To make sending with a different adapter more dynamic, as you asked:

    defmodule MyApp.Mailer do
      # Map all your defined mailers here
      @adapters %{
        mandrill: MyApp.MandrillMailer,
        send_grid: MyApp.SendGridMailer
      }

      def for(adapter \\ :mandrill) do
        Map.fetch!(@adapters, adapter)
      end
    end

    # Mail service can be stored in db record
    mail_service = :send_grid
    Email.welcome_email |> MyApp.Mailer.for(mail_service).deliver_now
1
votes

You would need to do this at runtime with the current Bamboo setup. There is an adapter for runtime configurations. I use this for the same reason you use it. I have to get the configuration out of a data store because the user of the software gets to change the configuration at runtime.

https://hexdocs.pm/bamboo_config_adapter/0.2.0/Bamboo.ConfigAdapter.html#content

I hope this helps. If you have any questions you can put them in the GitHub issues for the library and I will be glad to help.