mikel / mail

A Really Ruby Mail Library
MIT License
3.63k stars 935 forks source link

Mail::Address.new parsing failed when string has a comma #1219

Open neohunter opened 6 years ago

neohunter commented 6 years ago

If the string to be parsed contains a comma in the name it throws the wong email address.

Mail::Address.new('Arnold, Roa <arnold@github.com>').address
=> "Arnold"
neohunter commented 6 years ago

While having an answer i'm writing the most ugly monkey patch ever:

class Mail::Address
  class << self
    def new(value = nil)
      value = value.try(:gsub, ',', ' ')
      super(value)
    end
  end
end

UPDATE Don't do this, it breaks mail gem.

rafbm commented 6 years ago

This looks like regular address parsing behavior. Commas , are used to separate addresses. If you want to create address fields that may contain commas (or other special characters), it would be safer to surround the name part with double quotes ".

Mail::Address.new('"Arnold, Roa" <arnold@github.com>').address
# => "arnold@github.com"

Mail::Address.new('"Arnold, Roa" <arnold@github.com>').name
# => "Arnold, Roa"
neohunter commented 6 years ago

Very useful... thanks. However unfortunatelly the address is what I receive from Griddler gem comming from mandrill..

Mail::Address is supposed to parse a single address, I think it should not consider commas as a mail separator here.

Svelix commented 5 years ago

I got a similar problem with some incoming mails: We get addresses in header fields that look like this:

Mail::AddressList.new("Doe, John <jon.doe@example.com>").addresses.map(&:format)
=> ["Doe", "John <jon.doe@example.com>"]

so they are split into multiple addresses, although they are meant to be only one.

Or even worse like this:

Mail::AddressList.new("John Doe, Example INC <john.doe@example.com>")
=> Mail::Field::IncompleteParseError: Mail::AddressList can not parse |John Doe, Example INC <john.doe@example.com>|: Only able to parse up to "John Doe"

that will throw exceptions while parsing.

I know, the name parts should usually be in quotes to prevent this, but unfortunately we cannot control which mail clients everybody uses and how weird some mail clients format addresses.

For the first case I wrote some post-processing code that "stitches" invalid email addresses together again, but in the second case I cannot do much cause of the exception thrown.

Any chances that cases like this can be handled by the parser somehow?

mojobiri commented 5 years ago

Have the same issue. Did anyone found a workaround?

Svelix commented 5 years ago

Since my previous workaround did not help in the second case I described, I now changed the parser and created a pull request: #1365

mattwalters commented 2 years ago

Are there any plans to merge @Svelix 's solution?