Skip to content

Commit

Permalink
Support email tagging
Browse files Browse the repository at this point in the history
Email tags allow you to categorize outgoing emails and get detailed
statistics about them. This change adds the `tag` function to
`PostmarkHelper`. This tag is later added to the HTTP request as the
`Tag` param.
  • Loading branch information
pablo-co authored Dec 29, 2016
1 parent 5f7b846 commit 436528f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ defmodule MyApp.Mail do
end
end
```

#### Exception Warning

Postmark templates include a subject, HTML body and text body and thus these shouldn't be included in the email as they will raise an API exception.
Expand All @@ -72,3 +73,20 @@ email
|> html_body("<p>Will raise exception</p>")
|> text_body("Will raise exception")
```

## Tagging emails

The Postmark adapter provides a helper module for tagging emails.

### Example

```elixir
defmodule MyApp.Mail do
import Bamboo.PostmarkHelper

def some_email do
email
|> tag("some-tag")
end
end
```
15 changes: 13 additions & 2 deletions lib/bamboo/postmark_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ defmodule Bamboo.PostmarkAdapter do
end

defp convert_to_postmark_params(email) do
email_params(email) |> maybe_put_template_params(email)
email
|> email_params()
|> maybe_put_template_params(email)
|> maybe_put_tag_params(email)
end

defp maybe_put_template_params(params, %{private:
Expand All @@ -92,6 +95,14 @@ defmodule Bamboo.PostmarkAdapter do
params
end

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

defp maybe_put_tag_params(params, _) do
params
end

defp email_params(email) do
recipients = recipients(email)
%{
Expand Down Expand Up @@ -119,7 +130,7 @@ defmodule Bamboo.PostmarkAdapter do

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

defp recipients(email) do
Expand Down
12 changes: 12 additions & 0 deletions lib/bamboo/postmark_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ defmodule Bamboo.PostmarkHelper do

alias Bamboo.Email

@doc """
Set a single tag for an email that allows you to categorize outgoing emails
and get detailed statistics.
A convenience function for `put_private(email, :tag, "my-tag")`
## Example
tag(email, "welcome-email")
"""
def tag(email, tag) do
Email.put_private(email, :tag, tag)
end

@doc """
Send emails using Postmark's template API.
Expand Down
8 changes: 8 additions & 0 deletions test/lib/bamboo/postmark_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ defmodule Bamboo.PostmarkAdapterTest do
"name" => 'example name'}]
end

test "deliver/2 puts tag param" do
email = new_email |> PostmarkHelper.tag("some_tag")

email |> PostmarkAdapter.deliver(@config)

assert_receive {:fake_postmark, %{params: %{"Tag" => "some_tag"}}}
end

test "raises if the response is not a success" do
email = new_email(from: "INVALID_EMAIL")

Expand Down

0 comments on commit 436528f

Please sign in to comment.