Skip to content

Zero-dependency, forgiving, e-mail address parser and formatter library for Elixir

License

Notifications You must be signed in to change notification settings

amberbit/email_address

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EmailAddress

EmailAddress provides top-level parsing & formatting API to deal with RFC5322-formatted e-mail addresses.

This library is meant to be very forgiving and deal with invalid / malformed e-mail addresses in a fashion allowing for a recovery and avoiding common errors that happen when clients send malformed emails.

The case for having a forgiving library to parse e-mail addresses, is that the e-mail clients do not stick to the rules or have bugs allowing for invalid e-mail addresses to be used when sending, receiving or processing e-mails.

This library defines e-mail address as a string, which contains what most people think as sender name + e-mail address.

When parsing, we support various formats encountered in the wild:

- [email protected]
- Jack Black <[email protected]>
- "Jack@Black" <[email protected]>
- Jack Black [email protected]
- Jack\n\rBlack\[email protected]
- [email protected] (Jack Black)
- Jack Black <[email protected]>

Installation

If available in Hex, the package can be installed by adding email_address to your list of dependencies in mix.exs:

def deps do
  [
    {:email_address, "~> 1.0"}
  ]
end

Documentation can be can be found at https://hexdocs.pm/email_address.

Usage

Parsing e-mail addresses:

EmailAddress.parse("[email protected]")
=> %EmailAddress.Address{addr_spec: "[email protected]", display_name: ""}

EmailAddress.parse("Jack Black <[email protected]>")
=> %EmailAddress.Address{addr_spec: "[email protected]", display_name: "Jack Black"}

EmailAddress.parse("Not an e-mail")
=> nil

Formatting e-mail address:

EmailAddress.format(%EmailAddress.Address{addr_spec: "[email protected]", display_name: ""})
=> "[email protected]"

EmailAddress.format(%EmailAddress.Address{addr_spec: "[email protected]", display_name: "Jack Black"})
=> "Jack Black <[email protected]>"

EmailAddress.format(%EmailAddress.Address{addr_spec: "[email protected]", display_name: "Jack <Black's> Email"})
=> "\\"Jack <Black's> Email\\" <[email protected]>"

Thanks

This code has been made open source thanks to Keeping.com. Please check them out if you need a shared Gmail inbox!

Copyright and License

Copyright (c) 2024, Hubert Łępicki.

EmailAddress source code is licensed under the MIT License.

About

Zero-dependency, forgiving, e-mail address parser and formatter library for Elixir

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages