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

Handle encoding type in lower case #17

Open
wants to merge 4 commits 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
2 changes: 1 addition & 1 deletion lib/mimemail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule MimeMail do
end
body = case headers[:'content-type'] do
{"multipart/"<>_,%{boundary: bound}}->
body |> String.split(~r"\s*--#{bound}\s*") |> Enum.slice(1..-2) |> Enum.map(&from_string/1) |> Enum.map(&decode_body/1)
body |> String.split(~r"\s*--#{Regex.escape(bound)}\s*") |> Enum.slice(1..-2) |> Enum.map(&from_string/1) |> Enum.map(&decode_body/1)
{"text/"<>_,%{charset: charset}} ->
body |> Iconv.conv(charset,"utf8") |> ok_or(ensure_ascii(body)) |> ensure_utf8
_ -> body
Expand Down
6 changes: 3 additions & 3 deletions lib/mimemail_headers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule MimeMail.Emails do
data |> String.strip |> String.split(~r/\s*,\s*/) |> Enum.map(&MimeMail.Address.decode/1)
end
def decode_headers(%MimeMail{headers: headers}=mail) do
parsed=for {k,{:raw,v}}<-headers, k in [:from,:to,:cc,:cci,:'delivered-to'] do
parsed=for {k,{:raw,v}}<-headers, k in [:sender,:from,:to,:cc,:cci,:'reply-to',:'delivered-to'] do
{k,v|>MimeMail.header_value|>parse_header}
end
%{mail| headers: Enum.reduce(parsed,headers, fn {k,v},acc-> Dict.put(acc,k,v) end)}
Expand Down Expand Up @@ -110,10 +110,10 @@ defmodule MimeMail.Words do

def single_word_decode("=?"<>rest = str) do
case String.split(rest,"?") do
[enc,"Q",enc_str,"="] ->
[enc,enc_type,enc_str,"="] when enc_type in ~w(q Q) ->
str = q_to_binary(enc_str,[])
MimeMail.ok_or(Iconv.conv(str,enc,"utf8"),MimeMail.ensure_ascii(str))
[enc,"B",enc_str,"="] ->
[enc,enc_type,enc_str,"="] when enc_type in ~w(b B)->
str = Base.decode64(enc_str) |> MimeMail.ok_or(enc_str)
MimeMail.ok_or(Iconv.conv(str,enc,"utf8"),MimeMail.ensure_ascii(str))
_ -> "#{str} "
Expand Down
8 changes: 5 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Mix.Tasks.Compile.Iconv do
use Mix.Task

@shortdoc "Compiles Iconv"
@doc """
For Linux:
Expand Down Expand Up @@ -36,10 +38,10 @@ defmodule Mailibex.Mixfile do
[app: :mailibex,
version: "0.1.2",
elixir: "> 1.0.0",
description: description,
package: package,
description: description(),
package: package(),
compilers: [:iconv, :elixir, :app],
deps: deps]
deps: deps()]
end

def application do
Expand Down
4 changes: 4 additions & 0 deletions test/mime_headers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ defmodule MimeHeadersTest do
test "decode str from base 64 encoded-word" do
assert "Jérôme Nicolle"
= MimeMail.Words.word_decode("=?UTF-8?B?SsOpcsO0bWUgTmljb2xsZQ==?=")
assert "Jérôme Nicolle"
= MimeMail.Words.word_decode("=?utf-8?b?SsOpcsO0bWUgTmljb2xsZQ==?=")
end

test "decode str from q-encoded-word" do
assert "[FRnOG] [TECH] ToS implémentée chez certains transitaires"
= MimeMail.Words.word_decode("[FRnOG] =?UTF-8?Q?=5BTECH=5D_ToS_impl=C3=A9ment=C3=A9e_chez_certa?=\r\n =?UTF-8?Q?ins_transitaires?=")
assert "[FRnOG] [TECH] ToS implémentée chez certains transitaires"
= MimeMail.Words.word_decode("[FRnOG] =?utf-8?q?=5BTECH=5D_ToS_impl=C3=A9ment=C3=A9e_chez_certa?=\r\n =?utf-8?q?ins_transitaires?=")
end

test "encode str into multiple encoded-word, test line length and round trip" do
Expand Down