在 Ruby 中的 ActionMailer 中将电子邮件标头转换为另一种编码
我有一个用 ruby 1.8.5 和 Rails 1.2.6 编写的网站。
有一个反馈页面。
所以。
我有一个模型类:
class Feedback::Notify < ActionMailer::Base
def answer_for_managers(question)
recipients [email protected]
from "[email protected]"
subject "Обратная связь: ответ на вопрос"
body "question" => question
content_type "text/html"
end
end
然后我有一个控制器:
class Feedback::QuestionController < Office::BaseController
def update
Feedback::Notify.deliver_answer_for_managers(@question)
end
end
问题是发送消息时其主题如下所示: =?utf-8?Q?=d0=9e=d0=b1=d1=80=d0=b0=d1=82=d0=bd=d0=b0=d1=8f_=d1=81=d0=b2=d1 =8f=d0=b7=d1=8 c=3a_=d0=a1=d0=be=d1=82=d1=80=d1=83=d0=b4=d0=bd=d0=b8=d0=ba_=d0=be=d1=82=d0= b2=d0=b5=d1=8 2=d0=b8=d0=bb_=d0=bd=d0=b0_=d0=b2=d0=be=d0=bf=d1=80=d0=be=d1=81_=d0=ba=d0=bb= d0=b8=d0=b5= d0=bd=d1=82=d0=b0_=23=35=36_=d0=be=d1=82_=32=36=2e=30=38=2e=32=30=31=31_=31=31= 3a=33=33?=
所以它是 url 编码的。
有什么方法可以防止将主题文本转换为 url 编码吗? 所有文件均为UTF8编码
I have a website written on ruby 1.8.5 and rails 1.2.6.
There is a feedback page.
So.
i've got a model class:
class Feedback::Notify < ActionMailer::Base
def answer_for_managers(question)
recipients [email protected]
from "[email protected]"
subject "Обратная связь: ответ на вопрос"
body "question" => question
content_type "text/html"
end
end
Then i have a controller:
class Feedback::QuestionController < Office::BaseController
def update
Feedback::Notify.deliver_answer_for_managers(@question)
end
end
The problem is when a message is sent its subject looks like: =?utf-8?Q?=d0=9e=d0=b1=d1=80=d0=b0=d1=82=d0=bd=d0=b0=d1=8f_=d1=81=d0=b2=d1=8f=d0=b7=d1=8c=3a_=d0=a1=d0=be=d1=82=d1=80=d1=83=d0=b4=d0=bd=d0=b8=d0=ba_=d0=be=d1=82=d0=b2=d0=b5=d1=82=d0=b8=d0=bb_=d0=bd=d0=b0_=d0=b2=d0=be=d0=bf=d1=80=d0=be=d1=81_=d0=ba=d0=bb=d0=b8=d0=b5=d0=bd=d1=82=d0=b0_=23=35=36_=d0=be=d1=82_=32=36=2e=30=38=2e=32=30=31=31_=31=31=3a=33=33?=
so it's url-encoded.
Is there any way to prevent converting subject text to url-encoding?
All files are in UTF8 encoding
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您将未转义的 UTF-8 字符放入标头字段中,则会违反相应的标准 RFC 822 和 RFC 5322 规定标头字段只能由(7 位)ASCII 字符组成。
因此,ActionMailer 在这里做了正确的事情并转义了 UTF-8 字符。由于标头中没有任何内容表明要使用另一种编码,因此接收者(以及所有中间服务器)除了遵循该标准之外没有其他机会,因为他们没有其他线索可能使用了哪种编码。
由于 RFC 822 相当旧(但对于电子邮件仍然具有权威性),UTF-8 只是不存在,正如它所指定的那样。转义是 RFC 2047 指定的解决方法,它准确指定了您在标头中看到的内容。 MUA 预计会取消转义文本并在渲染时显示正确的字形。
请注意,完全可以在消息正文内发送 unicode 文本(大多数时候在 MIME 容器内)。在那里可以使用附加标头指定实际数据编码和传输编码。请参阅 RFC 2045 ff。了解更多详情。
请阅读 RFC 或查看有关 Unicode 和电子邮件。
If you would put unescaped UTF-8 characters into header fields, you would be violating the respective standards RFC 822 and RFC 5322 which state that header fields can only be composed of (7-bit) ASCII characters.
Thus, ActionMailer does the right thing here and escapes the UTF-8 characters. As nothing in the headers states that another encoding is to be used, the recipient (and all intermediate servers) have no other chance than to follow that standard as they have no other clue which encoding might have been used.
As RFC 822 is rather old (but still authoritative for email), UTF-8 just didn't exist as it was specified. The escaping is a workaround specified by RFC 2047 which exactly specifies what you see in the header. MUAs are expected to unescape the text and display the proper glyphs on rendering.
Note that it is entirely possible to send unicode text inside the message body (most of the time inside a MIME container). There it is possible to specify the actual data encoding and the transport encoding using additional headers. See RFC 2045 ff. for more details.
Please read either the RFCs or have a look at the Wikipedia entry on Unicode and e-mail.
我通过添加默认的 'Content-Transfer-Encoding' => 解决了我的问题我的 ActionMailer 中的“7bit”
查看 API 文档
i solve my problem by adding default 'Content-Transfer-Encoding' => '7bit' in my ActionMailer
have a look in the API docs