0.0
Low commit activity in last 3 years
A long-lived project that still receives updates
A practical mail address parser implemented based on Perl Module Mail::Address.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 0.22
~> 2.1
~> 3.1, >= 3.1.0
~> 13.0
 Project Readme

MailAddress Test Coverage Status

MailAddress is a Ruby port of Perl's Mail::Address, a widely used email address parser. While the mail gem is excellent, it cannot parse certain email addresses. Mail::Address can conveniently parse even non-RFC-compliant email addresses such as:

# mail gem cannot parse the following addresses
Ello [Do Not Reply] <do-not-reply@ello.co> # [, ] are not permitted according to RFC5322
大阪 太郎<osaka@example.com> # no whitespace just before `<`

However, Mail::Address (Perl) has some limitations that MailAddress addresses:

  • Fails to parse correctly when the name part has no closing parenthesis.
  • Over-modifies the name part.

Many people copy and paste email addresses from Excel or other spreadsheets, where addresses are separated by whitespace (tab or space). To handle this, MailAddress also includes a parser ported from the Google Closure Library.

Installation

Add this line to your application's Gemfile:

gem 'mail_address'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mail_address

Usage

The API is almost the same as Mail::Address (Perl). Note that the comment property was removed from the address class in v1.0.0, since most users are unlikely to need it.

require 'mail_address'

line = "John 'M' Doe <john@example.com> (this is a comment), 大阪 太郎 <osaka@example.jp>"
addrs = MailAddress.parse(line)

p addrs[0].format     # "\"John 'M' Doe (this is a comment)\" <john@example.com>"
p addrs[0].address    # "john@example.com"
p addrs[0].name       # "John 'M' Doe (this is a comment)"
p addrs[0].phrase     # "John 'M' Doe (this is a comment)"
p addrs[0].host       # "example.com"
p addrs[0].user       # "john"
p addrs[0].original   # "John 'M' Doe <john@example.com> (this is a comment)"

p addrs[1].format     # "\"大阪 太郎\" <osaka@example.jp>"
p addrs[1].address    # "osaka@example.jp"
p addrs[1].name       # "大阪 太郎"
p addrs[1].phrase     # "大阪 太郎"
p addrs[1].host       # "example.jp"
p addrs[1].user       # "osaka"
p addrs[1].original   # "大阪 太郎 <osaka@example.jp>"

address.name and address.phrase are almost the same. address.phrase keeps outermost double quotes or parentheses.

If you only need a single address, you can use parse_first.

line = "John Doe <john@example.com>"
addr = MailAddress.parse_first(line)

p addr.address # "john@example.com"

Parse addresses separated with whitespace

require 'mail_address'

line = "John Doe <john@example.com> second@example.com, third@example.com" # separated with space and comma
addrs = MailAddress.parse_simple(line)

Contributing

  1. Fork it ( https://github.com/kizashi1122/mail_address/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request