Mail::Message.new fails to parse UTF8 name in the :to field
Error and backtrace when calling 'deliver' on a Message object that failed to parse a UTF8 name in the :to key in the initializer.
NoMethodError: undefined method 'encoding' for ["\"á\" <[email protected]>"]:Array
[GEM_ROOT]/gems/mail-2.2.19/lib/mail/fields/unstructured_field.rb:75:in 'do_decode'
[GEM_ROOT]/gems/mail-2.2.19/lib/mail/fields/unstructured_field.rb:56:in 'decoded'
[GEM_ROOT]/gems/mail-2.2.19/lib/mail/fields/unstructured_field.rb:60:in 'default'
[GEM_ROOT]/gems/mail-2.2.19/lib/mail/field.rb:123:in 'method_missing'
[GEM_ROOT]/gems/mail-2.2.19/lib/mail/message.rb:1107:in 'default'
[GEM_ROOT]/gems/mail-2.2.19/lib/mail/message.rb:1081:in 'to'
[GEM_ROOT]/gems/actionmailer-3.0.10/lib/action_mailer/base.rb:440:in 'set_payload_for_mail'
[GEM_ROOT]/gems/actionmailer-3.0.10/lib/action_mailer/base.rb:425:in 'block in deliver_mail'
[GEM_ROOT]/gems/activesupport-3.0.10/lib/active_support/notifications.rb:52:in 'block in instrument'
[GEM_ROOT]/gems/activesupport-3.0.10/lib/active_support/notifications/instrumenter.rb:21:in 'instrument'
[GEM_ROOT]/gems/activesupport-3.0.10/lib/active_support/notifications.rb:52:in 'instrument'
[GEM_ROOT]/gems/actionmailer-3.0.10/lib/action_mailer/base.rb:424:in 'deliver_mail'
[GEM_ROOT]/gems/mail-2.2.19/lib/mail/message.rb:230:in 'deliver'
Sample Code
m1 = Mail::Message.new(to: '"á" <[email protected]>')
m1.header[:to].field.class
#=> Mail::UnstructuredField
m2 = Mail::Message.new
m2[:to] = '"á" <[email protected]>'
m2.header[:to].field.class
#=> Mail::ToField
I ran into this same error when addressing a message to '"" <[email protected]>'. The error occurred whenever I called message.cc (I was using the cc field, but I'm sure it would be the same for the to field). Even if this is invalid input for a recipient (ToField) — and it probably is — the error message should be made more helpful.
m1 = Mail::Message.new(to: '"" <[email protected]>')
i ran into the same issue with an invalid configured email like <released <[email protected]>
the problem is that in field.rb all parse-errors get swallowed and an UnstructuredField gets created instead of a ToField
def create_field(name, value, charset)
begin
self.field = new_field(name, value, charset)
rescue Mail::Field::ParseError => e
self.field = Mail::UnstructuredField.new(name, value)
self.field.errors << [name, value, e]
self.field
end
end
one would expect this to print AT LEAST a warning!
the underlying error is clear and should have been raised instead of swalled:
Mail::Field::ParseError: AddressListsParser can not parse |Released <released <[email protected]>|
Reason was: Expected one of
, (, @, > at line 1, column 20 (byte 20) after <released
i would like to provide a patch for this, but i don't know how to handle it otherwise, that's why i just provide a failing test.
We occasionally have the same error in our Rails 3.1.1 / Ruby 1.9.2 app :
undefined method `encoding' for #<Array:0x000008143a15d0>
mail (2.3.0) lib/mail/fields/unstructured_field.rb:75:in `do_decode'
....
Did you find a way to patch this ?
the problem that i experienced were invalid e-mail-addresses like <released <[email protected]> (mind the missing <)
there is no way to patch this, you need to find the broken ones (by using a regex to validate).
I'm getting the same error as @flackou for bcc. Can an invalid email id cause this issue ? I had an email id alpha_num_chars_followed_by_single_dot@domain which is invalid because the dot cannot be at the end.
While using bcc, is the mail sent to correct email ids, if some of the email ids in the list are invalid ?
undefined method `encoding' for #<Array:0xd70251c>
/home/blah/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/mail-2.3.0/lib/mail/fields/unstructured_field.rb:75:in `do_decode'
/home/blah/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/mail-2.3.0/lib/mail/fields/unstructured_field.rb:56:in `decoded'
/home/blah/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/mail-2.3.0/lib/mail/fields/unstructured_field.rb:60:in `default'
/home/blah/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/mail-2.3.0/lib/mail/field.rb:123:in `method_missing'
/home/blah/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/mail-2.3.0/lib/mail/message.rb:1107:in `default'
/home/blah/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/mail-2.3.0/lib/mail/message.rb:476:in `bcc'
/home/blah/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/actionmailer-3.1.3/lib/action_mailer/base.rb:448:in `set_payload_for_mail'
/home/blah/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/actionmailer-3.1.3/lib/action_mailer/base.rb:431:in `block in deliver_mail'
/home/blah/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/notifications.rb:53:in `block in instrumen\
t'
/home/blah/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/notifications/instrumenter.rb:21:in `instr\
ument'
/home/blah/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/notifications.rb:53:in `instrument'
/home/blah/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/actionmailer-3.1.3/lib/action_mailer/base.rb:430:in `deliver_mail'
/home/blah/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/mail-2.3.0/lib/mail/message.rb:230:in `deliver'
@phoet, what do you mean by "there is no way to patch this"?
Don't you think a patch should be applied that causes errors such as these to be _raised_ instead of swallowed??
Mail::Field::ParseError: AddressListsParser can not parse |Released <released <[email protected]>|
Reason was: Expected one of
, (, @, > at line 1, column 20 (byte 20) after <released
This seems like a bug to me, in that it causes strange side effects (parse-errors get swallowed and an UnstructuredField gets created instead of a ToField) and can be a headache to debug and try to find the actual cause of the problem.
Today I ran into the other manifestation of this bug mentioned by @flackou, which is this error:
undefined method `encoding' for #<Array:0x000008143a15d0>
mail (2.3.0) lib/mail/fields/unstructured_field.rb:75:in `do_decode'
This error too is quite unhelpful. If it had at least shown me the contents of the array (["Name <>", ...] instead of #<Array:0x000008143a15d0> then at least I could have spotted the invalid address by manual inspection.
@TylerRick you are totally right! what i meant is there is no easy way for me to find out how to patch this without breaking all the stuff:-)
I am having a similar issue to @flackou on Ruby 1.9.2 / Rails 3.1.4 on all attempts to deliver mail (not just the devise mailer in the trace below)
undefined method `encoding' for ["text/plain", "text/enriched", "text/html"]:Array
mail (2.3.3) lib/mail/fields/unstructured_field.rb:75:in `do_decode'
mail (2.3.3) lib/mail/fields/unstructured_field.rb:56:in `decoded'
mail (2.3.3) lib/mail/fields/unstructured_field.rb:107:in `wrapped_value'
mail (2.3.3) lib/mail/fields/unstructured_field.rb:70:in `do_encode'
mail (2.3.3) lib/mail/fields/unstructured_field.rb:52:in `encoded'
mail (2.3.3) lib/mail/field.rb:123:in `method_missing'
mail (2.3.3) lib/mail/header.rb:190:in `block in encoded'
mail (2.3.3) lib/mail/header.rb:189:in `each'
mail (2.3.3) lib/mail/header.rb:189:in `encoded'
mail (2.3.3) lib/mail/message.rb:1708:in `encoded'
actionmailer (3.1.4) lib/action_mailer/base.rb:451:in `set_payload_for_mail'
actionmailer (3.1.4) lib/action_mailer/base.rb:431:in `block in deliver_mail'
activesupport (3.1.4) lib/active_support/notifications.rb:53:in `block in instrument'
activesupport (3.1.4) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.1.4) lib/active_support/notifications.rb:53:in `instrument'
actionmailer (3.1.4) lib/action_mailer/base.rb:430:in `deliver_mail'
mail (2.3.3) lib/mail/message.rb:230:in `deliver'
devise (2.1.0) lib/devise/models/confirmable.rb:128:in `send_on_create_confirmation_instructions'
Mail delivery was working without problems until recently, I am unable to figure out what changed to cause this problem.
Adding empty functions for encoding and encode! to Array lets me deliver mail again, but obviously that is not a proper solution to whatever is going on.
class Array
def encoding
end
def encode!(x)
x
end
end
Did anyone ever figure out what is causing this? I'm going through a rails2->3 upgrade right now and this has me stuck. Error very similar to @fignuts "undefined method `encoding' for ["text/plain", "text/html"]:Array".
Any help is appreciated.
I too am seeing this problem when attempting to send mail to an invalid address. It needs to raise Mail::Field::ParseError instead of NoMethodError on Array.
Same issue here.
OK, the problem here is not that the ToField should accept an array, it's that the Email address parser can't handle UTF-8 as it only works on ASCII.
What we need to do is either:
- rewrite the parser to handle UTF-8 (a lot of work)
- pre-encode the incoming string so that it is ASCII before sending it to the parser (also hard, but maybe less so).
The other error of a < in the email address is a separate issue.
I'll review this after I close off some of the other pull requests.
I think this may have been fixed with PR #358 Please reopen if this does not fix your problems on accepting UTF-8.
The random < character in the To field really needs it's own spec failure and work done on the parser. It's a tough one. But closing this ticket as resolved as the original problem was to do with UTF-8
This is similar to #744.
Fixed by #1103
everything will get fixed eventually :) thx!