DockYard / elixir-mail

Build composable mail messages
392 stars 64 forks source link

Add support for invalid chars #147

Closed thiamsantos closed 1 year ago

thiamsantos commented 2 years ago

Motivation

We are receiving some email in which the filenames are not in utf-8. Causing the following exception:

** (FunctionClauseError) no function clause matching in Mail.Parsers.RFC2822.parse_quoted_string/2

The following arguments were given to Mail.Parsers.RFC2822.parse_quoted_string/2:

    # 1
    <<250, 100, 101, 95, 45, 95, 51, 49, 45, 48, 53, 45, 50, 50, 95, 45, 9, 95, 65, 98, 114, 105, 108, 46, 112, 100, 102, 46, 106, 112, 103, 34>>

    # 2
    "Boleto_Porto_Seguro_Sa"

    (mail 0.2.3) lib/mail/parsers/rfc_2822.ex:315: Mail.Parsers.RFC2822.parse_quoted_string/2
    (mail 0.2.3) lib/mail/parsers/rfc_2822.ex:297: Mail.Parsers.RFC2822.parse_structured_header_value/4
    (mail 0.2.3) lib/mail/parsers/rfc_2822.ex:200: Mail.Parsers.RFC2822.parse_headers/2
    (mail 0.2.3) lib/mail/parsers/rfc_2822.ex:21: Mail.Parsers.RFC2822.parse/1
    (elixir 1.13.1) lib/enum.ex:1593: Enum."-map/2-lists^map/1-0-"/2
    (elixir 1.13.1) lib/enum.ex:1593: Enum."-map/2-lists^map/1-0-"/2
    (mail 0.2.3) lib/mail/parsers/rfc_2822.ex:388: Mail.Parsers.RFC2822.parse_body/2

Changes proposed in this pull request

Ignore the unsupported characters when parsing the header value.

Honestly I don't know if the proposed changes make sense to be in the library, probably a proper charset handling as mentioned in #78 would be better.

bcardarella commented 2 years ago

In order to add this to the RFC2822 parser we'd need confirmation that this doesn't violate the spec, or even better, it aligns us more closely with it. I don't have availability to review that at the moment but if you found out one way or the other that would be much appreciated!