Skip to content

Commit fc0453c

Browse files
committed
Fix remaining parameter bugs, fix attachment download, some cleanup
1 parent da6b48d commit fc0453c

File tree

7 files changed

+73
-46
lines changed

7 files changed

+73
-46
lines changed

lib/mailman/attachment.ex

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -666,17 +666,18 @@ defmodule Mailman.Attachment do
666666
]
667667

668668
defp get_attachment_data_via_http(url) do
669-
case is_valid_url?(url) do
670-
{:ok, _} ->
671-
response = HTTPotion.get(url, timeout: 60_000)
672-
669+
if is_valid_url?(url) do
670+
with {:ok, response} <- HTTPoison.get(url, recv_timeout: 60_000) do
673671
case response.status_code do
674672
200 -> {:ok, response.body}
675673
_ -> {:error, :invalid_http_response}
676674
end
677-
678-
{:error, message} ->
679-
{:error, message}
675+
else
676+
{:error, message} ->
677+
{:error, message}
678+
end
679+
else
680+
{:error, :invalid_url}
680681
end
681682
end
682683

@@ -749,14 +750,13 @@ defmodule Mailman.Attachment do
749750
def mime_type_and_subtype_from_extension(path) do
750751
extension = Path.extname(path)
751752

752-
type =
753-
Enum.find(mime_types(), fn {ext, _} ->
754-
ext == extension
755-
end)
756-
757-
case type do
753+
mime_types()
754+
|> Enum.find(fn {ext, _} ->
755+
ext == extension
756+
end)
757+
|> case do
758758
nil -> "application/octet-stream"
759-
_ -> elem(type, 1)
759+
{_ext, mimetype} -> mimetype
760760
end
761761
|> String.split("/")
762762
|> List.to_tuple()

lib/mailman/parsing.ex

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,27 @@ defmodule Mailman.Parsing do
2323

2424
@doc "Parses the message and returns Email"
2525
def parse!(message_text) do
26-
case parse message_text do
27-
{ :ok, parsed } -> parsed
28-
{ :error, reason } -> throw "Couldn't parse given message. #{reason}"
26+
case parse(message_text) do
27+
{:ok, parsed} -> parsed
28+
{:error, reason} -> throw("Couldn't parse given message. #{reason}")
2929
end
3030
end
3131

3232
def get_header(raw, name) do
33-
header = Enum.find all_headers(raw), fn({header_name, _}) ->
34-
header_name == name
35-
end
33+
header =
34+
Enum.find(get_headers(raw), fn {header_name, _} ->
35+
header_name == name
36+
end)
37+
3638
if header != nil do
3739
value = elem(header, 1)
40+
3841
cond do
39-
name == "To" || name == "Cc" || name == "Bcc" -> String.split(value, ",") |> Enum.map(&String.trim(&1))
40-
true -> value
42+
name == "To" || name == "Cc" || name == "Bcc" ->
43+
String.split(value, ",") |> Enum.map(&String.trim(&1))
44+
45+
true ->
46+
value
4147
end
4248
else
4349
if name == "To" || name == "Cc" || name == "Bcc" do
@@ -48,32 +54,34 @@ defmodule Mailman.Parsing do
4854
end
4955
end
5056

51-
def all_headers(raw) do
52-
elem(raw, 2)
57+
def get_headers({_mime_type, _mime_subtype, headers, _parameters, _content}) do
58+
headers
5359
end
5460

5561
def filename_from_raw(raw_part) do
56-
maybe_param = raw_parameters_for(raw_part) |>
57-
List.last |>
58-
elem(1) |>
59-
Enum.find(fn(p) ->
62+
maybe_param =
63+
raw_part
64+
|> get_parameters
65+
|> Map.get(:disposition_params)
66+
|> Enum.find(fn p ->
6067
elem(p, 0) == "filename"
6168
end)
69+
6270
if maybe_param != nil do
6371
elem(maybe_param, 1)
6472
else
6573
nil
6674
end
6775
end
6876

69-
def raw_parameters_for(raw_part) do
70-
elem(raw_part, 3)
77+
def get_parameters({_mime_type, _mime_subtype, _headers, parameters, _content}) do
78+
parameters
7179
end
7280

73-
def is_raw_attachement(raw_part) do
81+
def is_raw_attachment(raw_part) do
7482
case filename_from_raw(raw_part) do
7583
nil -> false
76-
_ -> true
84+
_ -> true
7785
end
7886
end
7987

@@ -88,11 +96,11 @@ defmodule Mailman.Parsing do
8896
def get_attachments(raw) do
8997
raw
9098
|> content_parts
91-
|> Enum.filter(&is_raw_attachement(&1))
92-
|> Enum.map(&raw_to_attachement(&1))
99+
|> Enum.filter(&is_raw_attachment(&1))
100+
|> Enum.map(&raw_to_attachment(&1))
93101
end
94102

95-
def raw_to_attachement(raw_part) do
103+
def raw_to_attachment(raw_part) do
96104
%Mailman.Attachment{
97105
file_name: filename_from_raw(raw_part),
98106
mime_type: get_type(raw_part),
@@ -103,11 +111,12 @@ defmodule Mailman.Parsing do
103111

104112
def content_parts(raw) when is_tuple(raw) do
105113
body = get_raw_body(raw)
114+
106115
cond do
107116
is_binary(body) -> [raw]
108-
is_list(body) -> Enum.map(body, &content_parts(&1))
117+
is_list(body) -> Enum.map(body, &content_parts(&1))
109118
end
110-
|> List.flatten
119+
|> List.flatten()
111120
end
112121

113122
def get_type(raw) when is_tuple(raw) do

lib/mailman/render.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ defmodule Mailman.Render do
1616
top_mime_type,
1717
top_mime_sub_type,
1818
headers_for(email) ++ extra_headers,
19-
[],
19+
%{},
2020
top_content_part
2121
}
2222
end
@@ -31,20 +31,20 @@ defmodule Mailman.Render do
3131
html_part_tuple
3232
else
3333
if is_nil(html_part_tuple), do: nil, else:
34-
{"multipart", "related", [], [], [html_part_tuple | inline_attachment_part_tuples]}
34+
{"multipart", "related", [], %{}, [html_part_tuple | inline_attachment_part_tuples]}
3535
end
3636

3737
alternative_or_plain_tuple = if is_nil(related_or_html_part_tuple) do
3838
plain_part_tuple
3939
else
40-
{"multipart", "alternative", [], [], [plain_part_tuple, related_or_html_part_tuple]}
40+
{"multipart", "alternative", [], %{}, [plain_part_tuple, related_or_html_part_tuple]}
4141
end
4242

4343
mixed_or_alternative_tuple = if Enum.empty?(attached_attachment_part_tuples) do
4444
alternative_or_plain_tuple
4545
else
4646
if is_nil(alternative_or_plain_tuple), do: nil, else:
47-
{"multipart", "mixed", [], [], [alternative_or_plain_tuple | attached_attachment_part_tuples]}
47+
{"multipart", "mixed", [], %{}, [alternative_or_plain_tuple | attached_attachment_part_tuples]}
4848
end
4949

5050
mixed_or_alternative_tuple

mix.exs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule Mailman.Mixfile do
1717

1818
# Configuration for the OTP application
1919
def application do
20-
[applications: [:ssl, :crypto, :eiconv, :gen_smtp, :httpotion]]
20+
[applications: [:ssl, :crypto, :eiconv, :gen_smtp, :httpoison]]
2121
end
2222

2323
# Note that :eiconv encoder/decoder is used by gen_smtp as well,
@@ -34,9 +34,10 @@ defmodule Mailman.Mixfile do
3434
defp deps do
3535
[
3636
{:eiconv, "~> 1.0.0"},
37-
{:gen_smtp, "~> 0.15.0"},
37+
# {:gen_smtp, "~> 0.15.0"},
38+
{:gen_smtp, github: "gen-smtp/gen_smtp", override: true},
3839
{:ex_doc, ">= 0.19.1", only: :dev},
39-
{:httpotion, "~> 3.1.0"},
40+
{:httpoison, "~> 1.6"},
4041
{:credo, "~> 0.10", only: [:dev, :test], runtime: false}
4142
]
4243
end

mix.lock

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
%{
22
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], []},
3+
"certifi": {:hex, :certifi, "2.5.2", "b7cfeae9d2ed395695dd8201c57a2d019c0c43ecaf8b8bcb9320b40d6662f340", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
34
"credo": {:hex, :credo, "0.10.0", "66234a95effaf9067edb19fc5d0cd5c6b461ad841baac42467afed96c78e5e9e", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, optional: false]}, {:jason, "~> 1.0", [hex: :jason, optional: false]}]},
45
"earmark": {:hex, :earmark, "1.3.0", "17f0c38eaafb4800f746b457313af4b2442a8c2405b49c645768680f900be603", [:mix], [], "hexpm"},
56
"eiconv": {:hex, :eiconv, "1.0.0", "ee1e47ee37799a05beff7a68d61f63cccc93101833c4fb94b454c23b12a21629", [:rebar3], []},
67
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
7-
"gen_smtp": {:hex, :gen_smtp, "0.15.0", "9f51960c17769b26833b50df0b96123605a8024738b62db747fece14eb2fbfcc", [:rebar3], [], "hexpm"},
8+
"gen_smtp": {:git, "https://github.com/gen-smtp/gen_smtp.git", "62f585bf8d630908c9f958c978140f1012be0343", []},
9+
"hackney": {:hex, :hackney, "1.16.0", "5096ac8e823e3a441477b2d187e30dd3fff1a82991a806b2003845ce72ce2d84", [:rebar3], [{:certifi, "2.5.2", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.1", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.0", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.6", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
10+
"httpoison": {:hex, :httpoison, "1.7.0", "abba7d086233c2d8574726227b6c2c4f6e53c4deae7fe5f6de531162ce9929a0", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
811
"httpotion": {:hex, :httpotion, "3.1.0", "14d20d9b0ce4e86e253eb91e4af79e469ad949f57a5d23c0a51b2f86559f6589", [:mix], [{:ibrowse, "~> 4.4", [hex: :ibrowse, optional: false]}]},
12+
"hut": {:hex, :hut, "1.3.0", "71f2f054e657c03f959cf1acc43f436ea87580696528ca2a55c8afb1b06c85e7", [:"erlang.mk", :rebar, :rebar3], [], "hexpm"},
913
"ibrowse": {:hex, :ibrowse, "4.4.0", "2d923325efe0d2cb09b9c6a047b2835a5eda69d8a47ed6ff8bc03628b764e991", [:rebar3], []},
14+
"idna": {:hex, :idna, "6.0.1", "1d038fb2e7668ce41fbf681d2c45902e52b3cb9e9c77b55334353b222c2ee50c", [:rebar3], [{:unicode_util_compat, "0.5.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
1015
"jason": {:hex, :jason, "1.1.1", "d3ccb840dfb06f2f90a6d335b536dd074db748b3e7f5b11ab61d239506585eb2", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, optional: true]}]},
1116
"makeup": {:hex, :makeup, "0.5.5", "9e08dfc45280c5684d771ad58159f718a7b5788596099bdfb0284597d368a882", [:mix], [{:nimble_parsec, "~> 0.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
1217
"makeup_elixir": {:hex, :makeup_elixir, "0.10.0", "0f09c2ddf352887a956d84f8f7e702111122ca32fbbc84c2f0569b8b65cbf7fa", [:mix], [{:makeup, "~> 0.5.5", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
18+
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
19+
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm"},
1320
"nimble_parsec": {:hex, :nimble_parsec, "0.4.0", "ee261bb53214943679422be70f1658fff573c5d0b0a1ecd0f18738944f818efe", [:mix], [], "hexpm"},
21+
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
22+
"ranch": {:hex, :ranch, "1.6.1", "2609724c9a7e163ca716a46c9087e037db4262935f5d866122ba158ed917c233", [:rebar3], [], "hexpm"},
23+
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm"},
24+
"unicode_util_compat": {:hex, :unicode_util_compat, "0.5.0", "8516502659002cec19e244ebd90d312183064be95025a319a6c7e89f4bccd65b", [:rebar3], [], "hexpm"},
1425
}

test/attachments_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ defmodule AttachmentsTest do
66
assert is_map(attachment)
77
end
88

9+
test "Attachment via HTTPoison works" do
10+
{:ok, attachment} = Mailman.Attachment.inline("https://www.w3.org/")
11+
assert is_map(attachment)
12+
end
13+
14+
915
test "#inline returns {:error, message} when file doesn't exist" do
1016
file_path = "test/data/idontexist.png"
1117
{:error, _} = Mailman.Attachment.inline(file_path)

test/mailman_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ defmodule MailmanTest do
120120
assert bcc_email.bcc |> length == 1
121121
end
122122

123-
test "encodes attachements properly" do
123+
test "encodes attachments properly" do
124124
email_with_attachments = email_with_attachments()
125125
{:ok, message} = MyApp.Mailer.deliver(email_with_attachments)
126126
email = Mailman.Email.parse!(message)

0 commit comments

Comments
 (0)