Rails 使用 Action Mailer 邮件功能中文编码问题

最近在学习使用rails 的 action mailer实现邮件的发送,当邮件渲染的模版中包含中文字符时,会由于编码问题,导致邮件功能出问题,调试了很长时间,最后发现,其实只要简单设置模版的编码格式就行了。代码如下:

系统默认的编码方式(不支持中文)

  • app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.user_mailer.password_reset.subject
  #
  def password_reset(user)
    @user = user
    mail to: @user.email, subject: '密码重置'
  end
end
  • app/views/user_mailer/password_reset.html.erb
<h1>密码重置</h1>

<p>点击以下链接,重置您的密码:</p>

<%= link_to "密码重置", edit_password_reset_url(@user.reset_token,
                                                      email: @user.email) %>

<p>链接两小时内有效.</p>

<p>
如果您不要需重置密码,请忽略此邮件!
</p>
  • 后台输出log
    有中文字符时,Content-Transfer-Encoding 默认采用了base64编码
----==_mimepart_5a2e6d05ed318_17da3fdfe8d207142933e
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: base64

PCFET0NUWVBFIGh0bWw+CjxodG1sPgogIDxoZWFkPgogICAgPG1ldGEgaHR0
cC1lcXVpdj0iQ29udGVudC1UeXBlIiBjb250ZW50PSJ0ZXh0L2h0bWw7IGNo
YXJzZXQ9dXRmLTgiIC8+CiAgICA8c3R5bGU+CiAgICAgIC8qIEVtYWlsIHN0
eWxlcyBuZWVkIHRvIGJlIGlubGluZSAqLwogICAgPC9zdHlsZT4KICA8L2hl
YWQ+CgogIDxib2R5PgogICAgPGgxPuWvhueggemHjee9rjwvaDE+Cgo8cD7n
grnlh7vku6XkuIvpk77mjqXvvIzph43nva7mgqjnmoTlr4bnoIE6PC9wPgoK
PGEgaHJlZj0iaHR0cDovL2xvY2FsaG9zdDozMDAwL3Bhc3N3b3JkX3Jlc2V0
cy9wNVF4SW5nN2FNV0RXVVJyODlWNUN3L2VkaXQ/ZW1haWw9d3V3ZWkyMTVh
JTQwMTI2LmNvbSI+5a+G56CB6YeN572uPC9hPgoKPHA+6ZO+5o6l5Lik5bCP
5pe25YaF5pyJ5pWILjwvcD4KCjxwPgrlpoLmnpzmgqjkuI3opoHpnIDph43n
va7lr4bnoIHvvIzor7flv73nlaXmraTpgq7ku7bvvIEKPC9wPgoKICA8L2Jv
ZHk+CjwvaHRtbD4K

----==_mimepart_5a2e6d05ed318_17da3fdfe8d207142933e--

Redirected to http://localhost:3000/
Completed 302 Found in 173ms (ActiveRecord: 5.1ms)

修改后的编码方式

  • 在渲染模版时,设置:content_transfer_encoding: "7bit"
class UserMailer < ApplicationMailer
  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.user_mailer.password_reset.subject
  #
  def password_reset(user)
    @user = user
    mail(to: @user.email,
         subject: '密码重置') do |format|
      format.html(content_transfer_encoding: "7bit") 
      format.text(content_transfer_encoding: "7bit")
    end
  end
end
  • 修改后的输出log
----==_mimepart_5a2e6e778d286_17da3fdfe8d207a029769
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
      /* Email styles need to be inline */
    </style>
  </head>

  <body>
    <h1>密码重置</h1>

<p>点击以下链接,重置您的密码:</p>

<a href="http://localhost:3000/password_resets/UXroLfc7KxIzZ6ZJnA/edit?email=wuwei215a%40126.com">密码重置</a>

<p>链接两小时内有效.</p>

<p>
如果您不要需重置密码,请忽略此邮件!
</p>

  </body>
</html>

----==_mimepart_5a2e6e778d286_17da3fdfe8d207a029769--

Redirected to http://localhost:3000/
Completed 302 Found in 157ms (ActiveRecord: 17.4ms)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容