DockYard / elixir-mail

Build composable mail messages
388 stars 63 forks source link

Mail.get_html fails with inline charset specifications #171

Open LetThereBeDwight opened 2 hours ago

LetThereBeDwight commented 2 hours ago

Version

0.4.0

Test Case

  test "get_html with singlepart and content-type with inline charset spec" do
    mail =
      Mail.put_html(Mail.build(), "<h1>Some HTML</h1>")
      |> Mail.Message.put_content_type("text/html; charset=UTF-8")

    assert Mail.get_html(mail) == mail
  end

Steps to reproduce

Run the test

Expected Behavior

That mail.get_html returns the message in the above case

Actual Behavior

Nil because no match

andrewtimberlake commented 2 hours ago

The correct code should be:

message
|> Mail.Message.put_content_type(["text/html", {"charset", "UTF-8"}])
LetThereBeDwight commented 1 hour ago

The correct code should be:

message
|> Mail.Message.put_content_type(["text/html", {"charset", "UTF-8"}])

That results in a different content-type spec for the Mail object - my example is mimicking an RFC2822 email that comes in with a specification like this:

    To: user@example.com
    From: me@example.com
    Reply-To: otherme@example.com
    Subject: Test Email
    Content-Type: text/html; charset=UTF-8;

    <h1>SOME HTML</h1>

The fact that it's coming via RFC2822 isn't actually relevant(the parser is working fine) so I boiled it down to a totally possible and valid content-type and found the issue in the underlying with undermatching valid specs on get_html -> this also aligns the get_text matches on content-type as an added bonus as they should be the same outside of the actual content-type part.