Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

phoenix 1.6-rc0 - render_body with atom do nothing #208

Closed
romaluca opened this issue Sep 3, 2021 · 13 comments
Closed

phoenix 1.6-rc0 - render_body with atom do nothing #208

romaluca opened this issue Sep 3, 2021 · 13 comments

Comments

@romaluca
Copy link

romaluca commented Sep 3, 2021

Hi, i upgraded my webapp to phoenix 1.6-rc0 and now the tests of mail sending are failing
I noticed that the cause is render body with atom parameter (:registration_confirm) do nothing. No error is reported:

new()
|> to({user.name, user.email})
|> from(@from)
|> subject(subject)
|> render_body(:registration_confirm, %{username: user.name, url: url, subject: subject})

But if i change this in:
new()
|> to({user.name, user.email})
|> from(@from)
|> subject(subject)
|> render_body("registration_confirm.html", %{username: user.name, url: url, subject: subject})

It works. In my app there are the "registration_confirm.html.eex" and "registration_confirm.text.eex"
Is not possible using atom any more?

@princemaple
Copy link
Member

princemaple commented Sep 3, 2021

Hi, could you try phoenix_swoosh 1.0.0-rc.0?

@princemaple
Copy link
Member

There should be no breaking change and should work with phoenix 1.6

@romaluca
Copy link
Author

romaluca commented Sep 3, 2021

I upgraded also phoenix_swoosh with:
{:phoenix_swoosh, git: "https://github.com/swoosh/phoenix_swoosh"},
but it the same.
The problem is that the render_body returns nothing and there are no logs.

@princemaple
Copy link
Member

Hi @romaluca I'm going to need more info to debug that. Testing with a toy project shows that it works properly with atoms.

Could you setup a minimal reproducible example?

What I got:

lib/test_swoosh/something.ex

defmodule TestSwoosh.Something do
  use Phoenix.Swoosh, view: TestSwoosh.MyView

  def hi do
    new()
    |> subject("what")
    |> to("test@test.com")
    |> from("test@test.com")
    |> render_body(:hi, name: "world")
  end
end

lib/test_swoosh/my_view.ex

defmodule TestSwoosh.MyView do
  use Phoenix.View, root: "lib/test_swoosh/templates"
end

lib/test_swoosh/templates/my/hi.html.eex

<h1>Hello, <%= @name %></h1>

lib/test_swoosh/templates/my/hi.text.eex

Hello, <%= @name %>
iex(1)> TestSwoosh.Something.hi
%Swoosh.Email{
  assigns: %{layout: false, name: "world"},
  attachments: [],
  bcc: [],
  cc: [],
  from: {"", "test@test.com"},
  headers: %{},
  html_body: "<h1>Hello, world</h1>\n",
  private: %{
    phoenix_layout: false,
    phoenix_template: "hi.text",
    phoenix_view: TestSwoosh.MyView
  },
  provider_options: %{},
  reply_to: nil,
  subject: "what",
  text_body: "Hello, world\r\n",
  to: [{"", "test@test.com"}]
}

@princemaple
Copy link
Member

render_body should always return the rendered email struct. There is not a case where it would return nothing.

@romaluca
Copy link
Author

romaluca commented Sep 3, 2021

i found the cause is the layout. I have this:
use Phoenix.Swoosh, view: ShuttertopWeb.UserMailerView, layout: {ShuttertopWeb.LayoutView, :email}

and these files for the common layout:

lib/shuttertop_web/templates/layout/email.html.eex
lib/shuttertop_web/templates/layout/email.txt.eex

If i remove this from the code it works:
layout: {ShuttertopWeb.LayoutView, :email}
I tried also to change the content of the layout files with only:
<%= render @view_module, @view_template, assigns %>
or
<%= @inner_content %>
But it doesn't work.

@romaluca
Copy link
Author

romaluca commented Sep 3, 2021

I tried the send with iex and there is an error:
[error] %ArgumentError{message: "errors were found at the given arguments:\n\n * 1st argument: not an iodata term\n"}

@princemaple
Copy link
Member

lib/shuttertop_web/templates/layout/email.txt.eex

Has this always been txt? Could you try text?

@princemaple
Copy link
Member

princemaple commented Sep 3, 2021

I added layout and still works. I did get an error, but a different one, if I named it email.txt.eex, though.

lib/test_swoosh/something.ex

defmodule TestSwoosh.Something do
  use Phoenix.Swoosh, view: TestSwoosh.MyView, layout: {TestSwoosh.LayoutView, :email}

  def hi do
    new()
    |> subject("what")
    |> to("test@test.com")
    |> from("test@test.com")
    |> render_body(:hi, name: "world")
  end
end

lib/test_swoosh/layout_view.ex

defmodule TestSwoosh.LayoutView do
  use Phoenix.View, root: "lib/test_swoosh/templates"
end

lib/test_swoosh/templates/layout/email.html.eex

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <%= @inner_content %>
  </body>
</html>

lib/test_swoosh/templates/layout/email.text.eex

TEST <%= @inner_content %>
iex(8)> TestSwoosh.Something.hi
%Swoosh.Email{
  assigns: %{layout: {TestSwoosh.LayoutView, "email.text"}, name: "world"},
  attachments: [],
  bcc: [],
  cc: [],
  from: {"", "test@test.com"},
  headers: %{},
  html_body: "<!DOCTYPE html>\n<html lang=\"en\">\n  <head>\n    <meta charset=\"UTF-8\" />\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n    <title>Document</title>\n  </head>\n  <body>\n<h1>Hello, world</h1>\n\n  </body>\n</html>\n",
  private: %{
    phoenix_layout: {TestSwoosh.LayoutView, :email},
    phoenix_template: "hi.text",
    phoenix_view: TestSwoosh.MyView
  },
  provider_options: %{},
  reply_to: nil,
  subject: "what",
  text_body: "TEST Hello, world\r\n\n",
  to: [{"", "test@test.com"}]
}

@romaluca
Copy link
Author

romaluca commented Sep 3, 2021

Excuse me, in my post i wanted write
lib/shuttertop_web/templates/layout/email.text.eex
Ok if it works to you, i'll check more my code.
Thanks anyway!

@romaluca romaluca closed this as completed Sep 3, 2021
@princemaple
Copy link
Member

Did you manage to figure out what the issue is?

@romaluca
Copy link
Author

romaluca commented Sep 4, 2021

Yes, i used the same LayoutView for web pages and email
Before phoenix 1.6 it worked.
I created another LayoutView for email and now it works again.
Thanks

@princemaple
Copy link
Member

Interesting. Thanks for the reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants