我在 I18n 环境中使用 ActionMailer 发送邮件时遇到错误
我的 User 类有一个属性语言,它包含他的首选语言环境。同时,页面还有一个由菜单下拉列表控制的 :locale。
我支持通过发送给用户的邮件进行密码重置操作。当用户语言为 :he 并且页面区域设置为 :en 时,发送的邮件将被保留。似乎是编码问题。有什么想法吗?
以下是相关的代码片段:
app/mailers/application.rb:
class ApplicationMailer < ActionMailer::Base
default from: "[email protected]"
layout 'mailer'
end
app/mailers/user_mailer.rb:
class UserMailer < ApplicationMailer
def password_reset(user)
@user = user
I18n.with_locale(@user.language) do
mail(
to: @user.email,
subject: I18n.t('user_mailer.reset_password.subject')
)
end
end
app/views/user_mailer/password_reset.html.erb:
<h1><%= t('user_mailer.reset_password.subject') %></h1>
<p><%= t('user_mailer.reset_password.content') %></p>
<%= link_to "Reset password", edit_password_reset_url(@user.reset_token,
locale: @user.language,
email: @user.email) %>
<p><%= t('user_mailer.reset_password.message') %></p>
有一个相同的 :he 布局 (app/views/user_mailer/password_reset.he.html.erb)
翻译保留在(下面除了来自 :en 文件:
config/locales/user_mailer.{en|he}.yml:
en:
user_mailer:
reset_password:
subject: "Password Reset"
greeting: "Hi %{name},"
content: "To reset your password click the link below"
message: "This link will expire in two hours."
我得到的垃圾如下(摘自服务器日志:
----==_mimepart_622c60f27661a_11a8ca1c473e3
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: base64
16LXnCDXnteg16og15zXl9eT16kg15DXqiDXlNeh15nXodee15AsINeZ16kg
15zXnNeX15XXpSDXotecINeU15zXmdeg16cg15TXntem15XXqNejOg0KDQpo
dHRwOi8vbG9jYWxob3N0OjMwMDAvaGUvcGFzc3dvcmRfcmVzZXRzL2dMWWZn
TnJtTGR3VHlKR3ZYZGF6b3cvZWRpdD9lbWFpbD1nLm1hdGlzc2glNDBnbWFp
bC5jb20NCg0K15TXnNeZ16DXpyDXmdek16fXoiDXkdeq15XXmiDXqdei16rX
这一直拖下去......
My User class has an attribute language, which holds his preferred locale. At the same time, pages have also a :locale controlled by a menu dropdown list.
I support a password_reset action via a mail that is sent to the user. When the User language is :he and the pages locale is :en the mail sent is garaged. Seems like an issue on encoding. Any ideas?
Here are the relevant code pieces:
app/mailers/application.rb:
class ApplicationMailer < ActionMailer::Base
default from: "[email protected]"
layout 'mailer'
end
app/mailers/user_mailer.rb:
class UserMailer < ApplicationMailer
def password_reset(user)
@user = user
I18n.with_locale(@user.language) do
mail(
to: @user.email,
subject: I18n.t('user_mailer.reset_password.subject')
)
end
end
app/views/user_mailer/password_reset.html.erb:
<h1><%= t('user_mailer.reset_password.subject') %></h1>
<p><%= t('user_mailer.reset_password.content') %></p>
<%= link_to "Reset password", edit_password_reset_url(@user.reset_token,
locale: @user.language,
email: @user.email) %>
<p><%= t('user_mailer.reset_password.message') %></p>
There is an identical :he layout (app/views/user_mailer/password_reset.he.html.erb)
The translations are kept in (below except is from the :en file:
config/locales/user_mailer.{en|he}.yml:
en:
user_mailer:
reset_password:
subject: "Password Reset"
greeting: "Hi %{name},"
content: "To reset your password click the link below"
message: "This link will expire in two hours."
The garbage that I get follows (excerpt from the server logs:
----==_mimepart_622c60f27661a_11a8ca1c473e3
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: base64
16LXnCDXnteg16og15zXl9eT16kg15DXqiDXlNeh15nXodee15AsINeZ16kg
15zXnNeX15XXpSDXotecINeU15zXmdeg16cg15TXntem15XXqNejOg0KDQpo
dHRwOi8vbG9jYWxob3N0OjMwMDAvaGUvcGFzc3dvcmRfcmVzZXRzL2dMWWZn
TnJtTGR3VHlKR3ZYZGF6b3cvZWRpdD9lbWFpbD1nLm1hdGlzc2glNDBnbWFp
bC5jb20NCg0K15TXnNeZ16DXpyDXmdek16fXoiDXkdeq15XXmiDXqdei16rX
This drags on and on...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
部署到生产环境后,发现问题实际上是编码问题。在生产过程中,邮件实际上已发送,并且采用正确的编码(和语言)。
因此,请小心那些不基于 ASCII 但需要 UTF-8 编码的语言。它们不会正确显示在 Rails (dev) 日志中。
After deployment to production, it turned out that the issue was actually encoding. On production, the mails are actually sent and they come in the proper encoding (and language).
So, be careful with languages which are not based on ASCII but need UTF-8 encodings. They will not appear properly in Rails (dev) logs.