Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
neohunter opened this issue Mar 23, 2018 · 7 comments · May be fixed by #1365
Open

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

neohunter opened this issue Mar 23, 2018 · 7 comments · May be fixed by #1365

Comments

@neohunter
Copy link

neohunter commented Mar 23, 2018

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
Copy link
Author

neohunter commented Mar 23, 2018

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
Copy link
Contributor

rafbm commented Mar 23, 2018

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
Copy link
Author

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
Copy link

Svelix commented Jan 24, 2019

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
Copy link

mojobiri commented Oct 9, 2019

Have the same issue. Did anyone found a workaround?

@Svelix Svelix linked a pull request Nov 12, 2019 that will close this issue
@Svelix
Copy link

Svelix commented Nov 12, 2019

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants