Giter Site home page Giter Site logo

thoughtbot / bamboo_phoenix Goto Github PK

View Code? Open in Web Editor NEW
14.0 4.0 8.0 31 KB

Bamboo library to use Phoenix for rendering emails

Home Page: https://hex.pm/packages/bamboo_phoenix

License: MIT License

Elixir 96.89% HTML 1.53% Shell 1.59%
elixir-phoenix bamboo elixir phoenix

bamboo_phoenix's Introduction

Bamboo.Phoenix Circle CI

Bamboo & Bamboo.Phoenix are part of the thoughtbot Elixir family of projects.

Bamboo.Phoenix is a library to use Phoenix's View rendering layer for your Bamboo emails. After installation, see the Bamboo.Phoenix docs for more information.

Installation

Make sure you have Bamboo installed. To install Bamboo.Phoenix, add it to your list of dependencies in mix.exs:

def deps do
  [
    {:bamboo_phoenix, "~> 1.0.0"}
  ]
end

Contributing

Before opening a pull request, please open an issue first.

Once we've decided how to move forward with a pull request:

$ git clone https://github.com/thoughtbot/bamboo_phoenix.git
$ cd bamboo_phoenix
$ mix deps.get
$ mix test
$ mix format

Once you've made your additions and mix test passes, go ahead and open a PR!

We run the test suite as well as formatter checks on CI. Make sure you are using the Elixir version defined in the .tool-versions file to have consistent formatting with what's being run on CI.

About thoughtbot

thoughtbot

Bamboo & Bamboo.Phoenix area maintained and funded by thoughtbot, inc. The names and logos for thoughtbot are trademarks of thoughtbot, inc.

We love open-source software, Elixir, and Phoenix. See our other Elixir projects, or hire our Elixir Phoenix development team to design, develop, and grow your product.

bamboo_phoenix's People

Contributors

germsvel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

bamboo_phoenix's Issues

Text template required even when using only `put_html_layout`

Hey there,

Firstly, just wanted to thank you for creating/maintaining the Bamboo family of libs, they really make email rendering easy!

I am using bamboo-phoenix to render email templates using Phoenix views.

I setup a basic example:

my_app_web/emails/invite.ex

defmodule MyAppWeb.InviteEmail do
  use Bamboo.Phoenix, view: MyAppWeb.EmailView

  def invite do
    new_email()
    |> from("[email protected]")
    |> put_html_layout({MyAppWeb.LayoutView, "email.html"})
    |> to("[email protected]")
    |> subject("You've been invited")
    |> render(:invite)
  end
end
my_app_web/views/email_view.ex

defmodule MyAppWeb.EmailView do
  use MyAppWeb, :view
end
my_app_web/templates/email.html.eex

<html>
  <body>
    <%= render @view_module, @view_template, assigns %>
  </body>
</html>
my_app_web/templates/email/invite.html.eex

<h1>You've been invited!</h1>

I have a basic test, just to ensure the email is successfully created, however, when I run the test I get the following error:

     ** (Phoenix.Template.UndefinedError) Could not render "invite.text" for MyAppWeb.EmailView, please define a matching clause for render/2 or define a template at "lib/my_app_web/templates/email/*". The following templates were compiled:

     * invite.html

     Assigns:

     %{view_module: MyAppWeb.EmailView, view_template: "invite.text"}

     Assigned keys: [:view_module, :view_template]
     
          stacktrace:
       (phoenix 1.5.7) lib/phoenix/template.ex:337: Phoenix.Template.raise_template_not_found/3
       (phoenix 1.5.7) lib/phoenix/view.ex:472: Phoenix.View.render_to_iodata/3
       (phoenix 1.5.7) lib/phoenix/view.ex:479: Phoenix.View.render_to_string/3
       (bamboo_phoenix 1.0.0) lib/bamboo_phoenix.ex:283: Bamboo.Phoenix.render_html_and_text_emails/1
       test/my_app_web/emails/invite_test.exs:5: (test)

If I add an invite.text.eex template, then the test runs just fine, however, I'm only calling put_html_layout({MyAppWeb.LayoutView, "email.html"}) from my email module, so not sure why it requires me to also have a text template.

Any help would be greatly appreciated!

Better local preview for text-only emails

Problem

The company I work for decided to only use text emails and when we preview them locally, we always have the HTML div first with no HTML at all and we have to scroll down to reach the TEXT email body.

Screen Shot 2022-06-03 at 18 53 37

Solution

This was first addressed in an issue from Bamboo library which was already solved, but here on Bamboo Phoenix we still use and older version of Bamboo which doesn't has this change.

I believe that the solution is just to update some dependencies, and making sure that tests won't break.

Cheers.

Support for Phoenix 1.7

With Phoenix 1.7 replacing Phoenix.View with Phoenix.Component (see docs in :phoenix_view), bamboo_phoenix will need to be updated. This update will likely need to involve a few steps:

  1. Setting an upper limit on the currently specified phoenix version (involves a version release i.e. 1.0.1)
  2. Releasing a new version (i.e. 2.0.0) that either:
    a. Conforms to the updated usage of Phoenix.Component
    b. Includes :phoenix_view as a direct dependency so that Phoenix.View.render_to_string/3 can still be called

IMO 2a is the path that should be taken but I'd appreciate a maintainer's input before heading down a particular path.

Shouldn't assigns have multiple signatures?

Currently Bamboo.Phoenix only has the following assign/3 function and not the typical assign/2:

  def assign(%{assigns: assigns} = email, key, value) do
    %{email | assigns: Map.put(assigns, key, value)}
  end

This makes it a little inconvenient to assign multiple values. It forces you to have to either implement assign/2 yourself or do the following.

new_email()
|> assign(item_one: 1)
|> assign(item_two: 1)
|> assign(item_three: 1)

It definitely surprised me and I have to spend a little time figuring out why the following wasn't working:

new_email()
|> assign(item_one: 1, item_two: 2, item_three: 3)

Shouldn't we have the same functions available as Bamboo.Template?

  def assign(%{assigns: assigns} = email, attrs) when is_map(attrs) or is_list(attrs) do
    %{email | assigns: Enum.into(attrs, assigns)}
  end

  def assign(%{assigns: assigns} = email, key, value) do
    %{email | assigns: Map.put(assigns, key, value)}
  end

There is actually quite a bit of duplication of what's in Bamboo.Template, that could be cleaned up, along with adding assign/2 by doing the following in Bamboo.Phoenix:

import Bamboo.Email

I'm glad to do a pull request if you guys agree with that solution. We could also just re-implement assign/2, but I would prefer not to duplicate the code.

Let me know your thoughts, thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.