开发者

How to force the character encoding of HTML e-mails in Rails 3?

开发者 https://www.devze.com 2023-04-07 09:10 出处:网络
I\'m using Rails 3.1 (3.1.1 RC1) and I have configured ActionMailer to use windows-1252 as default encoding. (External requirement)

I'm using Rails 3.1 (3.1.1 RC1) and I have configured ActionMailer to use windows-1252 as default encoding. (External requirement)

This works perfectly with plain text mails, but as soon as I send HTML mails the text is converted to UTF-8 again resulting in garbled text.

Here's what I've done/found out.

  • I configured the default encoding: ActionMailer::Base.default :charset => 'windows-1252'
  • My .erb template is actually windows-1252 encoded.
  • I added the r开发者_Go百科equired marker <%# encoding: windows-1252 -%> as the first line of the template.
  • The mail has a correct content type header: Content-Type: text/html; charset="windows-1252"

Here's the code snippet I'm using to send the mail:

mail(:to => ..., :subject => "...") do |format|
  format.html
end    

I suspect that somehow during mail processing Rails/ActionMailer decides to convert the characters to UTF-8. How can I change that?


You don't mention what version of Ruby you are using (1.9.x do things differently then 1.8.x,) but assuming you are using a 1.9 version, you can set the following in your application.rb:

config.encoding = "windows-1252"

Which will set an application wide encoding. (which is utf-8 by default)


You should try to use the charset option within the mailer.

See here: http://railsdispatch.com/posts/actionmailer

0

精彩评论

暂无评论...
验证码 换一张
取 消