Karta.Mail.Charset (Karta v1.0.0)

Converts text in the charsets that make up nearly all mail to UTF-8, in pure Elixir.

  • utf-8 (also utf8): returned as is when valid.
  • us-ascii (and no charset at all): ASCII as is. Mail labelled ASCII that carries 8-bit text is common; it is read as UTF-8 when valid and as Windows-1252 otherwise.
  • iso-8859-1 (and its aliases) and windows-1252: decoded as Windows-1252, the superset that mail labelled ISO-8859-1 nearly always is (as web browsers do).

Any other charset returns {:error, :unsupported_charset}; callers keep the bytes and the label. No conversion goes through a C library.

Summary

Functions

Converts data in charset to UTF-8.

Like to_utf8/2, but text that cannot be converted is read as Windows-1252.

Functions

to_utf8(data, charset)

@spec to_utf8(binary(), String.t() | nil) ::
  {:ok, String.t()} | {:error, :unsupported_charset | :invalid}

Converts data in charset to UTF-8.

to_utf8_lossy(data, charset)

@spec to_utf8_lossy(binary(), String.t() | nil) :: String.t()

Like to_utf8/2, but text that cannot be converted is read as Windows-1252.