{"componentChunkName":"component---src-templates-lesson-js","path":"/phoenix-chat/transactional-email-with-mailgun-and-bamboo","result":{"data":{"mdx":{"body":"function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"Transactional Email with Mailgun and Bamboo\",\n  \"order\": 37,\n  \"free\": false,\n  \"active\": true,\n  \"tech\": [\"phoenix\", \"elixir\", \"mailgun\"]\n};\n\nvar makeShortcode = function makeShortcode(name) {\n  return function MDXDefaultShortcode(props) {\n    console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n    return mdx(\"div\", props);\n  };\n};\n\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, [\"components\"]);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h1\", null, \"Transactional Email with Mailgun and Bamboo\"), mdx(\"ul\", null, mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Set up Mailgun\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Set up Bamboo\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Send welcome email\")), mdx(\"p\", null, \"The Elixir ecosystem is rich with libraries offering us the ability to send emails such as \", mdx(\"a\", {\n    \"href\": \"https://github.com/swoosh/swoosh\",\n    \"parentName\": \"p\"\n  }, \"Swoosh\"), \" and \", mdx(\"a\", {\n    \"href\": \"https://github.com/thoughtbot/bamboo\",\n    \"parentName\": \"p\"\n  }, \"Bamboo\"), \". For our application we'll be using \", mdx(\"a\", {\n    \"href\": \"https://github.com/thoughtbot/bamboo\",\n    \"parentName\": \"p\"\n  }, \"Bamboo\"), \" by Thoughtbot which has support for composable emails and many different email providers. Bamboo also has really good \", mdx(\"a\", {\n    \"href\": \"https://hexdocs.pm/bamboo/readme.html\",\n    \"parentName\": \"p\"\n  }, \"documentation\"), \" that's worth looking over.\"), mdx(\"p\", null, \"Sending emails is one of the ways we see Elixir sets itself apart from other languages like Ruby. With great support for concurrency we won't need to include other dependencies to enable our applications to send emails in the background.\"), mdx(\"h2\", null, \"Install Bamboo\"), mdx(\"p\", null, \"The first thing we need to do is add our newest dependency, \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \":bamboo\"), \" to our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"mix.exs\"), \" file:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"...\\n  defp deps do\\n    [\\n      {:bamboo, \\\"~> 0.7\\\"},\\n     ...\\n\\n   ]\\n  end\\n...\\n\")), mdx(\"p\", null, \"Next we need to add \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \":bamboo\"), \" to our application function in \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"mix.exs\"), \":\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"def application do\\n  [mod: {PhoenixChat, []},\\n    applications: [\\n      :bamboo,\\n      ...\\n\\n  ]]\\nend\\n\")), mdx(\"p\", null, \"Then be sure to get your dependencies.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ mix deps.get\\n\")), mdx(\"h2\", null, \"Configuration\"), mdx(\"p\", null, \"One of the best features of Bamboo is its support for different providers.  For our project we'll be using \", mdx(\"a\", {\n    \"href\": \"http://www.mailgun.com/\",\n    \"parentName\": \"p\"\n  }, \"Mailgun\"), \" but if you have a strong preference for another provider, the configuration should be similar.\"), mdx(\"p\", null, \"Let's open up \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"config/dev.exs\"), \" and add a section for Bamboo at the bottom.  This configuration will be used only in development mode and informs Bamboo as to which adapter or provider to use:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"config :phoenix_chat, PhoenixChat.Mailer,\\n  adapter: Bamboo.LocalAdapter\\n\")), mdx(\"p\", null, \"The \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Bamboo.LocalAdapter\"), \" is great for development.  Rather than sending emails over the network and through our providers, the emails are captured locally and available for previewing.\"), mdx(\"p\", null, \"The other configurations need to be updated to include an appropriate Bamboo config.  For testing we'll want to use the special \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Bamboo.TestAdapter\"), \", which handles email in memory and does not send emails, and in production we'll use the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Bamboo.MailgunAdapter\"), \".\"), mdx(\"p\", null, \"For a complete list of adapters see the Bamboo docs: \", mdx(\"a\", {\n    \"href\": \"https://github.com/thoughtbot/bamboo#adapters\",\n    \"parentName\": \"p\"\n  }, \"Adapters\"), \".\"), mdx(\"p\", null, \"Open \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"config/test.exs\"), \" and add the following:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"config :phoenix_chat, PhoenixChat.Mailer,\\n  adapter: Bamboo.TestAdapter\\n\")), mdx(\"p\", null, \"For the production configuration we'll use Mailgun and pull the API key and Mailgun domain from our system environment variables, which we will define later:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"config :phoenix_chat, PhoenixChat.Mailer,\\n  adapter: Bamboo.MailgunAdapter,\\n  api_key: System.get_env(\\\"MAILGUN_API_KEY\\\"),\\n  domain: System.get_env(\\\"MAILGUN_DOMAIN\\\")\\n\")), mdx(\"h2\", null, \"Defining Emails\"), mdx(\"p\", null, \"Now that we've installed Bamboo and configured it, we need to create our Mailer and define our emails.\"), mdx(\"p\", null, \"Create a new file, \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"lib/phoenix_chat/mailer.ex\"), \", so we can define our Mailer module:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ touch lib/phoenix_chat/mailer.ex\\n\")), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"defmodule PhoenixChat.Mailer do\\n  use Bamboo.Mailer, otp_app: :phoenix_chat\\nend\\n\")), mdx(\"p\", null, \"The \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Mailer\"), \" is used for dispatching the emails to the adapter but construction of the Emails is done elsewhere. Let's create an \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Email\"), \" module \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"lib/phoenix_chat/email.ex\"), \" to construct our emails.\"), mdx(\"p\", null, \"Phoenix is not necessary to use Bamboo, but we can use the \", mdx(\"a\", {\n    \"href\": \"https://github.com/thoughtbot/bamboo#using-phoenix-views-and-layouts\",\n    \"parentName\": \"p\"\n  }, \"special adapter\"), \" to make email formatting easier down the road. It also comes with \", mdx(\"a\", {\n    \"href\": \"https://github.com/thoughtbot/bamboo#previewing-sent-emails\",\n    \"parentName\": \"p\"\n  }, \"this handy tool\"), \" for previewing formatted emails. If you don't need formatted emails, you can simply use \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"import Bamboo.Email\"), \" rather than \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Bamboo.Phoenix\"), \".\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ touch lib/phoenix_chat/email.ex\\n\")), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"defmodule PhoenixChat.Email do\\n  use Bamboo.Phoenix, view: PhoenixChat.EmailView\\n\\nend\\n\")), mdx(\"p\", null, \"Next we'll need to create our first email.  In Bamboo, emails are defined as functions and are built up using a helpful \", mdx(\"a\", {\n    \"href\": \"https://en.wikipedia.org/wiki/Domain-specific_language\",\n    \"parentName\": \"p\"\n  }, \"domain-specific language\"), \" (DSL). If you're not familiar with DSLs, you can think of them as tiny, special-purpose languages or libraries that are really good at doing one thing--in this case, email.\"), mdx(\"p\", null, \"To get started, we'll define a basic welcome email:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"defmodule PhoenixChat.Email do\\n  use Bamboo.Phoenix, view: PhoenixChat.EmailView\\n\\n  alias PhoenixChat.{User}\\n\\n  def welcome_email(%User{email: email}) do\\n    new_email\\n    |> to(email)\\n    |> from(\\\"no-reply@phoenixchat.io\\\")\\n    |> subject(\\\"Welcome\\\")\\n    |> text_body(\\\"Welcome to PhoenixChat!\\\")\\n  end\\nend\\n\")), mdx(\"p\", null, \"Bamboo's API is pretty easy. As you can see in the above code we specify the email to send to, the from address, subject, and then the text body.\"), mdx(\"p\", null, \"That's it for creating an email, now we need to send it.\"), mdx(\"h2\", null, \"Sending Emails\"), mdx(\"p\", null, \"With Bamboo there are two ways to send emails within the current request and in the background.  To send an email within the current request, we use the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"deliver_now/1\"), \" function on our mailer.  This function will dispatch the email request immediately and will need a response before the HTTP request can continue.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"alias PhoenixChat.{Email, Mailer}\\n\\nuser |> Email.welcome_email |> Mailer.deliver_now\\n\")), mdx(\"p\", null, \"To send our emails in the background we can use the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"deliver_later/1\"), \" function in Bamboo:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"user |> Email.welcome_email |> Mailer.deliver_later\\n\")), mdx(\"p\", null, \"The advantage of sending in the background is we won't block the current request, this has the least impact on the user's experience and it's what we will use almost exclusively.\"), mdx(\"p\", null, \"With our emails created and configured, we can proceed with integrating the welcome email into Phoenix.  Let's start by opening \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"web/controller/user_controller.ex\"), \" and creating a new method, \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"send_welcome_email/\"), \", to handle sending the email.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"alias PhoenixChat.{Email, Mailer, User}\\n\\n  ...\\n\\n  defp send_welcome_email(user) do\\n    user\\n    |> Email.welcome_email\\n    |> Mailer.deliver_later\\n  end\\n...\\n\")), mdx(\"p\", null, \"Once we've created our function we can incorporate it into \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"create/2\"), \", which is the function that gets called when we create a new user.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-elixir\",\n    \"parentName\": \"pre\"\n  }, \"  ...\\n\\n  def create(conn, %{\\\"user\\\" => user_params}) do\\n    changeset = User.registration_changeset(%User{}, user_params)\\n\\n    case Repo.insert(changeset) do\\n      {:ok, user} ->\\n        {:ok, token, _claims} = Guardian.encode_and_sign(user, :token)\\n\\n        send_welcome_email(user)\\n\\n        ...\\n    end\\n  end\\n\")), mdx(\"p\", null, \"That's it!  Now our users will receive a simple welcome email when they register for a new account. If you sign up, you should see something along the lines of the following in your server logs.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"%Bamboo.Email{assigns: %{}, bcc: [], cc: [],\\nfrom: {nil, \\\"no-reply@phoenixchat.io\\\"}, headers: %{},\\nhtml_body: nil, private: %{}, subject: \\\"Welcome\\\",\\ntext_body: \\\"Welcome to PhoenixChat!\\\",\\nto: [nil: \\\"info@learnphoenix.io\\\"]}\\n\")));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"Transactional Email with Mailgun and Bamboo Set up Mailgun Set up Bamboo Send welcome email The Elixir ecosystem is rich with libraries…","frontmatter":{"title":"Transactional Email with Mailgun and Bamboo","order":37,"active":true,"free":false,"tech":["phoenix","elixir","mailgun"]}}},"pageContext":{"slug":"transactional-email-with-mailgun-and-bamboo","prevNode":{"frontmatter":{"title":"Style the Chat Component","active":true,"free":false,"order":36,"tech":["react"]},"fields":{"slug":"style-the-chat-component"}},"nextNode":{"frontmatter":{"title":"Testing with Elixir and Phoenix: Controllers","active":true,"free":false,"order":38,"tech":["phoenix","elixir"]},"fields":{"slug":"testing-with-elixir-and-phoenix-controllers"}}}},"staticQueryHashes":["1723017916"]}