NoMethodError 当 “to” 包含无法解析字符的名称

huangapple go评论70阅读模式
英文:

NoMethodError when "to" contains a name with unparsable characters

问题

以下是您要翻译的内容:

We get people's names from a third party source, some of the last names contain a department suffix such as "(R&D)". When trying to send email, via ActionMailer, we get a NoMethodError error originating in the mail gem.

It can be reproduced without ActionMailer as follows:

to = "Kris Leech <kris@example.com>"
mail = Mail.new { to(to) }
mail[:to].addrs # => [#<Mail::Address]

to = "Kris Leech [TECH] <kris@example.com>"
mail = Mail.new { to(to) }
mail[:to].addrs # => NoMethodError: undefined method 'addrs' for Mail::UnstructuredField

The presence of the square brackets cause Mail::UnstructuredField, which does not respond to addrs, to be returned instead of Mail::StructuredField.

We can see that there is a parse error:

mail[:to].errors # => [["To", "Kris Leech [TECH] <kris.leech@example.com>", #<Mail::Field::IncompleteParseError: Mail::AddressList can not parse |Kris Leech [TECH] <kris@example.com>|: Only able to parse up to "Kris Leech ">]]

How do I escape or otherwise correct the name part of the "to"?

英文:

We get people's names from a third party source, some of the last names contain a department suffix such as &quot;(R&amp;D)&quot;. When trying to send email, via ActionMailer, we get a NoMethodError error originating in the mail gem.

It can be reproduced without ActionMailer as follows:

to = &quot;Kris Leech &lt;kris@example.com&gt;&quot;
mail = Mail.new { to(to) }
mail[:to].addrs # =&gt; [#&lt;Mail::Address]

to = &quot;Kris Leech [TECH] &lt;kris@example.com&gt;&quot;
mail = Mail.new { to(to) }
mail[:to].addrs # =&gt; NoMethodError: undefined method `addrs&#39; for Mail::UnstructuredField

The presence of the square brackets cause Mail::UnstructuredField, which does not respond to addrs, to be returned instead of Mail::StructuredField.

We can see that there is a parse error:

mail[:to].errors # =&gt; [[&quot;To&quot;, &quot;Kris Leech [TECH] &lt;kris.leech@example.com&gt;&quot;, #&lt;Mail::Field::IncompleteParseError: Mail::AddressList can not parse |Kris Leech [TECH] &lt;kris@example.com&gt;|: Only able to parse up to &quot;Kris Leech &quot;&gt;]]

How do I escape or otherwise correct the name part of the "to"?

答案1

得分: 2

根据规范,名称部分在包含保留字符时必须加引号。

这在Mail::Utilities中实现。

因此,我们可以像这样“引用”名称部分:

QuoteName = Class.new do
  extend Mail::Utilities

  def self.call(name)
    quote_phrase(name)
  end
end

to = "#{QuoteName.('Kris Leech [TECH]')} <kris@example.com>"
mail = Mail.new { to(to) }
mail[:to].addrs # => [#<Mail::Address]
英文:

As per the specification the name part, when it contains reserved characters, must be quoted.

This is implemented in Mail::Utilities.

So we can "quote" the name part as such:

QuoteName = Class.new do
  extend Mail::Utilities

  def self.call(name)
    quote_phrase(name)
  end
end

to = &quot;#{QuoteName.(&#39;Kris Leech [TECH]&#39;)} &lt;kris@example.com&gt;&quot;
mail = Mail.new { to(to) }
mail[:to].addrs # =&gt; [#&lt;Mail::Address]

huangapple
  • 本文由 发表于 2023年4月6日 19:10:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75948847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定