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.new fails to parse UTF-8 characters in to: field #685

Closed
jurezove opened this issue Mar 5, 2014 · 12 comments
Closed

Mail.new fails to parse UTF-8 characters in to: field #685

jurezove opened this issue Mar 5, 2014 · 12 comments

Comments

@jurezove
Copy link

jurezove commented Mar 5, 2014

mail = Mail.new(to: "Jure Žove <jure.pase.kure@gmail.com>")
mail[:to]

 => #<Mail::Field:0x007fe5ef506f90 @field=#<Mail::UnstructuredField:0x007fe5ef4ef520 @errors=[["to", "Jure Žove <jure.pase.kure@gmail.com>", #<Mail::Field::ParseError: Mail::AddressListsParser can not parse |Jure Žove <jure.pase.kure@gmail.com>|
Reason was: Expected one of 
, (, !, #, $, %, &, ', *, +, -, /, =, ?, ^, _, `, {, |, }, ~, ", ., @, :, <, ,, ; at line 1, column 6 (byte 6) after >]], @charset=#<Encoding:UTF-8>, @name="to", @length=nil, @tree=nil, @element=nil, @value="Jure Žove <jure.pase.kure@gmail.com>">> 
@carsonreinke
Copy link
Contributor

Mail::Address.new("Jure Žove <jure.pase.kure@gmail.com>")
Mail::Field::ParseError: Mail::AddressListsParser can not parse |Jure Žove <jure.pase.kure@gmail.com>|
Mail::Address.new("=?UTF-8?B?SnVyZSDFvW92ZQ==?= <jure.pase.kure@gmail.com>")
 => #<Mail::Address:70093092354000 Address: |"Jure Žove" <jure.pase.kure@gmail.com>| > 

Maybe this is expected, since the display name must be encoded.

@jurezove
Copy link
Author

jurezove commented Mar 5, 2014

Thanks, can you please provide a way how I could encode it and still preserve the character?

@carsonreinke
Copy link
Contributor

$ > addr = Mail::Address.new('jure.pase.kure@gmail.com')
 => #<Mail::Address:70197914477980 Address: |jure.pase.kure@gmail.com| > 
$ > addr.display_name = 'Jure Žove'
 => "Jure Žove" 
$ > addr.encoded()
 => "=?UTF-8?B?SnVyZSDFvW92ZQ==?= <jure.pase.kure@gmail.com>" 
$ > addr.decoded()
 => "\"Jure Žove\" <jure.pase.kure@gmail.com>" 

@jurezove
Copy link
Author

jurezove commented Mar 5, 2014

What about if I already receive a mail want get the display name of the to or from fields? I would probably need to encode the string ("Jure Žove email@email.com") before I try to parse it?

@carsonreinke
Copy link
Contributor

Look at the original unmodified email, the UTF-8 characters should be encoded.

Like I received an email notification from GitHub and it had this From: =?UTF-8?B?SnVyZSDFvW92ZQ==?= <notifications@github.com>

@jurezove
Copy link
Author

jurezove commented Mar 5, 2014

The thing is I'm getting this through a JSON request from Mailgun which is already in UTF-8 (not encoded).

@carsonreinke
Copy link
Contributor

Ultimately, the decoded address would have to be parsed. The Mail::Address comments state "Mail::Address requires a correctly formatted email address per RFC2822 or RFC822."

@jurezove
Copy link
Author

jurezove commented Mar 7, 2014

Okay, thanks! Will look into that.

@jurezove jurezove closed this as completed Mar 7, 2014
@carsonreinke
Copy link
Contributor

Related to #39

@sk-
Copy link

sk- commented Aug 10, 2015

Why was this issue closed? I'm also having troubles parsing these malformed headers.

One of the offending headers I'm getting is: From: ÁREA DE DEPORTES Y RECREACIÓN<foo@foo.com>

Note that Gmail is able to correctly parse this header, and that it is not encoded.

@adamsanderson
Copy link

@sk- If you are trying to use Mail::Address.new(addr), it will fail on non ascii characters.
Instead, try calling encoded = Mail::Encodings.address_encode(addr) and then use that with Mail::Address.new(addr). That will base64 encode the address with the string encoding embedded.

@jeremy
Copy link
Collaborator

jeremy commented May 15, 2017

These are fixed by #1103:

>> Mail.new(to: "Jure Žove <jure.pase.kure@gmail.com>").to
=> ["jure.pase.kure@gmail.com"]
>> Mail.new("From: ÁREA DE DEPORTES Y RECREACIÓN<foo@foo.com>").from
=> ["foo@foo.com"]

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

No branches or pull requests

5 participants