-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathraw.ex
286 lines (254 loc) · 6.5 KB
/
raw.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
defmodule BambooSes.Render.Raw do
@moduledoc """
Functions for rendering email messages into strings.
"""
# TODO render according to logic table: text, html, has_attachments, has_content_id_attachments
@doc """
Returns a tuple with all data needed for the underlying adapter to send.
"""
def render(email, extra_headers \\ []) do
has_text = String.length(email.text_body) > 0
has_html = String.length(email.html_body) > 0
has_attachments = length(filter_regular_attachments(email)) > 0
has_inline_attachments = length(filter_inline_attachments(email)) > 0
headers = headers_for(email) ++ extra_headers
build_parts(
has_text,
has_html,
has_attachments,
has_inline_attachments,
email,
headers
)
# |> IO.inspect()
|> :mimemail.encode()
# has_attachments =
# email
# # Returns a list of tuples
# |> compile_parts()
# # Nests the tuples and attaches necessary metadata
# |> nest_parts(email, extra_headers)
# |> :mimemail.encode()
end
defp build_parts(false, false, _, _, email, headers) do
{
"multipart",
"mixed",
headers,
%{},
prepare_attachments(email.attachments)
}
end
defp build_parts(false, true, false, false, email, headers) do
{
"text",
"html",
headers,
parameters_for(nil),
email.html_body
}
end
defp build_parts(false, true, false, true, email, headers) do
{
"multipart",
"related",
headers,
%{},
[
# generates html
build_parts(false, true, false, false, email, [])
] ++ prepare_attachments(filter_inline_attachments(email))
}
end
defp build_parts(false, true, true, false, email, headers) do
{
"multipart",
"mixed",
headers,
%{},
[
# generates html
build_parts(false, true, false, false, email, [])
] ++ prepare_attachments(email.attachments)
}
end
defp build_parts(false, true, true, true, email, headers) do
{
"multipart",
"mixed",
headers,
%{},
[
# generates html
build_parts(false, true, false, true, email, [])
] ++ prepare_attachments(filter_regular_attachments(email))
}
end
defp build_parts(true, false, false, false, email, headers) do
{
"text",
"plain",
headers,
parameters_for(nil),
email.text_body
}
end
defp build_parts(true, false, _, _, email, headers) do
{
"multipart",
"mixed",
headers,
%{},
[
# generates text
build_parts(true, false, false, false, email, [])
] ++ prepare_attachments(email.attachments)
}
end
defp build_parts(true, true, false, false, email, headers) do
{
"multipart",
"alternative",
headers,
%{},
[
# generates text
build_parts(true, false, false, false, email, []),
# generates html
build_parts(false, true, false, false, email, [])
]
}
end
defp build_parts(true, true, false, true, email, headers) do
{
"multipart",
"related",
headers,
%{},
[
# generates alternative
build_parts(true, true, false, false, email, [])
] ++ prepare_attachments(filter_inline_attachments(email))
}
end
defp build_parts(true, true, true, false, email, headers) do
{
"multipart",
"mixed",
headers,
%{},
[
# generates alternative
build_parts(true, true, false, false, email, [])
] ++ prepare_attachments(email.attachments)
}
end
defp build_parts(true, true, true, true, email, headers) do
{
"multipart",
"mixed",
headers,
%{},
[
# generates related with alternative
build_parts(true, true, false, true, email, [])
] ++ prepare_attachments(filter_regular_attachments(email))
}
end
defp prepare_attachments(attachments) do
attachments
|> Enum.map(fn attachment -> {:attachment, attachment.data, attachment} end)
|> attachment_part_tuples()
end
def filter_inline_attachments(email) do
Enum.filter(email.attachments, fn
attachment ->
!is_nil(attachment) &&
!is_nil(attachment.content_id) &&
String.length(attachment.content_id) > 0
end)
end
def filter_regular_attachments(email) do
Enum.filter(email.attachments, fn
attachment ->
!is_nil(attachment) &&
(is_nil(attachment.content_id) || String.length(attachment.content_id) == 0)
end)
end
@spec attachment_part_tuples([tuple()]) :: list(tuple())
defp attachment_part_tuples(parts) do
parts
|> Enum.filter(fn
{_, _, attachment} -> !is_nil(attachment)
_ -> false
end)
|> Enum.map(fn part ->
{
mime_type_for(part),
mime_subtype_for(part),
headers_for(part),
parameters_for(part),
elem(part, 1)
}
end)
end
defp mime_type_for({_type, _}) do
"text"
end
defp mime_type_for({_, _, attachment}) do
attachment.content_type
|> String.split("/")
|> List.first()
end
defp mime_subtype_for({type, _}) do
type
end
defp mime_subtype_for({_, _, attachment}) do
attachment.content_type
|> String.split("/")
|> List.last()
end
defp parameters_for({:attachment, _body, attachment}) do
%{
transfer_encoding: "base64",
content_type_params: [],
disposition: "attachment",
disposition_params: [{"filename", attachment.filename}]
}
end
defp parameters_for(_part) do
%{
transfer_encoding: "quoted-printable",
content_type_params: [],
disposition: "inline",
disposition_params: []
}
end
defp headers_for({:plain, _body}), do: []
defp headers_for({:html, _body}), do: []
defp headers_for({:attachment, _body, %{disposition: "inline"} = attachment}) do
attachment_id = URI.encode(attachment.file_name)
[
{"Content-ID", "<#{attachment_id}@bamboo_ses.attachment>"},
{"X-Attachment-Id", attachment_id}
]
end
defp headers_for({:attachment, _body, attachment}) do
if attachment.content_id do
[
{"Content-ID", attachment.content_id}
]
else
[]
end
end
defp headers_for(email) do
headers =
[
{"From", mailbox(email.from)},
{"Subject", email.subject}
] ++ Map.to_list(email.headers)
Enum.filter(headers, fn i -> elem(i, 1) != "" end)
end
defp mailbox({name, address}), do: "#{name} <#{address}>"
end