-
Notifications
You must be signed in to change notification settings - Fork 344
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
Mandrill Template Support #176
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
defmodule Bamboo.MandrillHelper do | ||
@moduledoc """ | ||
Functions for using features specific to Mandrill e.g. tagging, merge vars | ||
Functions for using features specific to Mandrill e.g. tagging, merge vars, templates | ||
""" | ||
|
||
alias Bamboo.Email | ||
|
@@ -48,4 +48,23 @@ defmodule Bamboo.MandrillHelper do | |
def tag(email, tags) do | ||
put_param(email, "tags", List.wrap(tags)) | ||
end | ||
|
||
@doc """ | ||
Send emails using Mandrill's template API. | ||
|
||
Setup Mandrill to send using a named template with template content. Use this | ||
in conjuction with merge vars to offload template rendering to Mandrill. The | ||
template name specified here must match the template name stored in Mandrill. | ||
Mandrill's API docs for this can be found [here](https://www.mandrillapp.com/api/docs/messages.JSON.html#method=send-template). | ||
|
||
## Example | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think of adding a link in between the example and the description that links to the Mandrill page about templates? |
||
|
||
template(email, "welcome") | ||
template(email, "welcome", [%{"name" => "Name", "content" => "John"}]) | ||
""" | ||
def template(email, template_name, template_content \\ []) do | ||
|> Email.put_private(:template_name, template_name) | ||
|> Email.put_private(:template_content, template_content) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,13 @@ defmodule Bamboo.MandrillAdapterTest do | |
end | ||
end | ||
|
||
post "/api/1.0/messages/send-template.json" do | ||
case get_in(conn.params, ["message", "from_email"]) do | ||
"INVALID_EMAIL" -> conn |> send_resp(500, "Error!!") |> send_to_parent | ||
_ -> conn |> send_resp(200, "SENT") |> send_to_parent | ||
end | ||
end | ||
|
||
defp send_to_parent(conn) do | ||
parent = Agent.get(__MODULE__, fn(set) -> HashDict.get(set, :parent) end) | ||
send parent, {:fake_mandrill, conn} | ||
|
@@ -78,6 +85,14 @@ defmodule Bamboo.MandrillAdapterTest do | |
assert request_path == "/api/1.0/messages/send.json" | ||
end | ||
|
||
test "deliver/2 sends the to the right url for templates" do | ||
new_email |> MandrillHelper.template("hello") |> MandrillAdapter.deliver(@config) | ||
|
||
assert_receive {:fake_mandrill, %{request_path: request_path}} | ||
|
||
assert request_path == "/api/1.0/messages/send-template.json" | ||
end | ||
|
||
test "deliver/2 sends from, html and text body, subject, and headers" do | ||
email = new_email( | ||
from: {"From", "[email protected]"}, | ||
|
@@ -126,6 +141,28 @@ defmodule Bamboo.MandrillAdapterTest do | |
assert message["important"] == true | ||
end | ||
|
||
test "deliver/2 puts template name and empty content" do | ||
email = new_email |> MandrillHelper.template("hello") | ||
|
||
email |> MandrillAdapter.deliver(@config) | ||
|
||
assert_receive {:fake_mandrill, %{params: %{"template_name" => template_name, "template_content" => template_content}}} | ||
assert template_name == "hello" | ||
assert template_content == [] | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might also be worth adding a test that makes sure the |
||
|
||
test "deliver/2 puts template name and content" do | ||
email = new_email |> MandrillHelper.template("hello", [ | ||
%{name: 'example name', content: 'example content'} | ||
]) | ||
|
||
email |> MandrillAdapter.deliver(@config) | ||
|
||
assert_receive {:fake_mandrill, %{params: %{"template_name" => template_name, "template_content" => template_content}}} | ||
assert template_name == "hello" | ||
assert template_content == [%{"content" => 'example content', "name" => 'example name'}] | ||
end | ||
|
||
test "raises if the response is not a success" do | ||
email = new_email(from: "INVALID_EMAIL") | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually the first sentence of docs should be the summary of the function. So maybe something like