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

Support the use of TemplateAlias #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 66 additions & 44 deletions lib/bamboo/postmark_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ defmodule Bamboo.PostmarkAdapter do
raise ArgumentError, """
There was no API key set for the Postmark adapter.
* Here are the config options that were passed in:
#{inspect config}
#{inspect(config)}
"""
end

Expand All @@ -87,30 +87,34 @@ defmodule Bamboo.PostmarkAdapter do

def maybe_put_attachments(params, %{attachments: attachments}) do
params
|> Map.put(:"Attachments", Enum.map(attachments, fn attachment ->
%{
Name: attachment.filename,
Content: attachment.data |> Base.encode64(),
ContentType: attachment.content_type,
ContentId: attachment.content_id
}
end))
end

defp maybe_put_template_params(params, %{private:
%{template_id: template_name, template_model: template_model}}) do
params
|> Map.put(:"TemplateId", template_name)
|> Map.put(:"TemplateModel", template_model)
|> Map.put(:"InlineCss", true)
|> Map.put(
:Attachments,
Enum.map(attachments, fn attachment ->
%{
Name: attachment.filename,
Content: attachment.data |> Base.encode64(),
ContentType: attachment.content_type,
ContentId: attachment.content_id
}
end)
)
end

defp maybe_put_template_params(params, %{private: %{template_model: template_model} = private}) do
case private do
%{template_id: template_id} -> Map.put(params, :TemplateId, template_id)
%{template_alias: template_alias} -> Map.put(params, :TemplateAlias, template_alias)
end
|> Map.put(:TemplateModel, template_model)
|> Map.put(:InlineCss, true)
end

defp maybe_put_template_params(params, _) do
params
end

defp maybe_put_tag_params(params, %{private: %{tag: tag}}) do
Map.put(params, :"Tag", tag)
Map.put(params, :Tag, tag)
end

defp maybe_put_tag_params(params, _) do
Expand All @@ -119,29 +123,39 @@ defmodule Bamboo.PostmarkAdapter do

defp email_params(email) do
recipients = recipients(email)
add_message_params(%{
"From": email_from(email),
"To": recipients_to_string(recipients, "To"),
"Cc": recipients_to_string(recipients, "Cc"),
"Bcc": recipients_to_string(recipients, "Bcc"),
"Subject": email.subject,
"TextBody": email.text_body,
"HtmlBody": email.html_body,
"Headers": email_headers(email),
"TrackOpens": true
}, email)

params = %{
From: email_from(email),
To: recipients_to_string(recipients, "To"),
Cc: recipients_to_string(recipients, "Cc"),
Bcc: recipients_to_string(recipients, "Bcc"),
Subject: email.subject,
Headers: email_headers(email),
TrackOpens: true
}

params =
case email do
%{private: %{template_id: _}} -> params
%{private: %{template_alias: _}} -> params
_ -> Map.merge(params, %{TextBody: email.text_body, HtmlBody: email.html_body})
end

add_message_params(params, email)
end

defp add_message_params(params, %{private: %{message_params: message_params}}) do
Enum.reduce(message_params, params, fn({key, value}, params) ->
Enum.reduce(message_params, params, fn {key, value}, params ->
Map.put(params, key, value)
end)
end

defp add_message_params(params, _), do: params

defp email_from(email) do
name = elem(email.from, 0)
email = elem(email.from, 1)

if name do
String.trim("#{name} <#{email}>")
else
Expand All @@ -150,8 +164,10 @@ defmodule Bamboo.PostmarkAdapter do
end

defp email_headers(email) do
Enum.map(email.headers,
fn {header, value} -> %{"Name": header, "Value": value} end)
Enum.map(
email.headers,
fn {header, value} -> %{Name: header, Value: value} end
)
end

defp recipients(email) do
Expand All @@ -162,35 +178,41 @@ defmodule Bamboo.PostmarkAdapter do
end

defp add_recipients(recipients, new_recipients, type: recipient_type) do
Enum.reduce(new_recipients, recipients, fn(recipient, recipients) ->
recipients ++ [%{
name: elem(recipient, 0),
email: elem(recipient, 1),
type: recipient_type
}]
Enum.reduce(new_recipients, recipients, fn recipient, recipients ->
recipients ++
[
%{
name: elem(recipient, 0),
email: elem(recipient, 1),
type: recipient_type
}
]
end)
end

defp recipients_to_string(recipients, type) do
recipients
|> Enum.filter(fn(recipient) -> recipient[:type] == type end)
|> Enum.map_join(",", fn(rec) -> "#{rec[:name]} <#{rec[:email]}>" end)
|> Enum.filter(fn recipient -> recipient[:type] == type end)
|> Enum.map_join(",", fn rec -> "#{rec[:name]} <#{rec[:email]}>" end)
end

defp headers(api_key) do
[{"accept", "application/json"},
{"content-type", "application/json"},
{"x-postmark-server-token", api_key}]
[
{"accept", "application/json"},
{"content-type", "application/json"},
{"x-postmark-server-token", api_key}
]
end

defp api_path(%{private: %{template_id: _}}), do: @send_email_template_path
defp api_path(%{private: %{template_alias: _}}), do: @send_email_template_path
defp api_path(_), do: @send_email_path

defp base_uri do
Application.get_env(:bamboo, :postmark_base_uri) || @default_base_uri
end

defp options(config) do
Keyword.merge(config[:request_options] || [], [with_body: true])
Keyword.merge(config[:request_options] || [], with_body: true)
end
end
12 changes: 11 additions & 1 deletion lib/bamboo/postmark_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,21 @@ defmodule Bamboo.PostmarkHelper do

template(email, "9746128")
template(email, "9746128", %{"name" => "Name", "content" => "John"})
template(email, {:alias, "my-template-alias"}, %{"name" => "Name", "content" => "John"})

"""
def template(email, template_id, template_model \\ %{}) do
email
|> Email.put_private(:template_id, template_id)
|> Email.put_private(:template_model, template_model)
|> put_private_template(template_id)
end

defp put_private_template(email, {:alias, template_alias}) do
Email.put_private(email, :template_alias, template_alias)
end

defp put_private_template(email, template_id) do
Email.put_private(email, :template_id, template_id)
end

@doc """
Expand All @@ -60,6 +69,7 @@ defmodule Bamboo.PostmarkHelper do
def put_param(%Email{private: %{message_params: _}} = email, key, value) do
put_in(email.private[:message_params][key], value)
end

def put_param(email, key, value) do
email
|> Email.put_private(:message_params, %{})
Expand Down