问题
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 required 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?
回答1:
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)
回答2:
You should try to use the charset option within the mailer.
See here: http://railsdispatch.com/posts/actionmailer
来源:https://stackoverflow.com/questions/7556173/how-to-force-the-character-encoding-of-html-e-mails-in-rails-3