forked from pablo-co/bamboo_postmark
-
Notifications
You must be signed in to change notification settings - Fork 1
/
postmark_adapter.ex
220 lines (181 loc) · 5.46 KB
/
postmark_adapter.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
defmodule Bamboo.PostmarkAdapter do
@moduledoc """
Sends email using Postmarks's API.
Use this adapter to send emails through Postmark's API. Requires that an API
key is set in the config.
## Example
# In config/config.exs, or config.prod.exs, etc.
config :my_app, MyApp.Mailer,
adapter: Bamboo.PostmarkAdapter,
api_key: "my_api_key" or {:system, "POSTMARK_API_KEY"}
"""
@behaviour Bamboo.Adapter
@default_base_uri "https://api.postmarkapp.com"
@send_email_path "email"
@send_email_template_path "email/withTemplate"
import Bamboo.ApiError, only: [build_api_error: 1]
def deliver(email, config) do
api_key = get_key(config)
params = email |> convert_to_postmark_params() |> json_library().encode!()
uri = [base_uri(), "/", api_path(email)]
case :hackney.post(uri, headers(api_key), params, options(config)) do
{:ok, status, _headers, response} when status > 299 ->
{:error, build_api_error(%{params: params, response: response})}
{:ok, status, headers, response} ->
{:ok, %{status_code: status, headers: headers, body: response}}
{:error, reason} ->
{:error, build_api_error(%{message: inspect(reason)})}
end
end
def handle_config(config) do
# build the api key - will raise if there are errors
Map.merge(config, %{api_key: get_key(config)})
end
@doc false
def supports_attachments?, do: true
defp get_key(config) do
api_key =
case Map.get(config, :api_key) do
{:system, var} -> System.get_env(var)
key -> key
end
if api_key in [nil, ""] do
raise_api_key_error(config)
else
api_key
end
end
def json_library do
Bamboo.json_library()
end
defp raise_api_key_error(config) do
raise ArgumentError, """
There was no API key set for the Postmark adapter.
* Here are the config options that were passed in:
#{inspect(config)}
"""
end
defp convert_to_postmark_params(email) do
email
|> email_params()
|> maybe_put_template_params(email)
|> maybe_put_tag_params(email)
|> maybe_put_attachments(email)
end
def maybe_put_attachments(params, %{attachments: []}) do
params
end
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)
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)
end
defp maybe_put_tag_params(params, _) do
params
end
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
)
end
defp add_message_params(params, %{private: %{message_params: message_params}}) do
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)
encode_name_and_email(name, email)
end
defp email_headers(email) do
Enum.map(
email.headers,
fn {header, value} -> %{Name: header, Value: value} end
)
end
defp recipients(email) do
[]
|> add_recipients(email.to, type: "To")
|> add_recipients(email.cc, type: "Cc")
|> add_recipients(email.bcc, type: "Bcc")
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
}
]
end)
end
defp recipients_to_string(recipients, type) do
recipients
|> Enum.filter(fn recipient -> recipient[:type] == type end)
|> Enum.map_join(",", &encode_name_and_email(&1.name, &1.email))
end
defp encode_name_and_email(name, email) do
encoded =
if name do
name = String.replace(name, ~s("), ~s(\\"))
~s("#{name}" <#{email}>)
else
email
end
String.trim(encoded)
end
defp headers(api_key) do
[
{"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(_), 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)
end
end