How to force character encoding of HTML email messages in Rails 3?

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

This works fine with regular text messages, but as soon as I send HTML letters, the text is converted to UTF-8 again, which leads to distortion of the text.

Here is what I did / found out.

  • I set the default encoding: ActionMailer::Base.default :charset => 'windows-1252'
  • My .erb template is actually windows-1252 encoded.
  • I added the marker <%# encoding: windows-1252 -%> as the first line of the template.
  • Mail has a valid content type header: Content-Type: text/html; charset="windows-1252" Content-Type: text/html; charset="windows-1252"

Here is a snippet of the code that I use to send mail:

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

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

+3
source share
2 answers

You do not specify which version of Ruby you are using (1.9.x does something different than 1.8.x,), but provided that you are using version 1.9, you can install the following in your .rb application:

 config.encoding = "windows-1252" 

A wide application encoding will be installed. (default is utf-8)

-1
source

You should try using the charset option in your mailbox.

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

-1
source

All Articles