ActionMailer HTML Encoding Hell - special characters replaced by garbage

I have a UTF-8 line: Website • Facebook
This is the bullet in the middle of aka • or 0xE2 0x80 0xA2

This value is correctly stored in the database and displayed correctly on the screen using Rails 3 and ruby ​​1.9.3 using the default settings.

I try to email this HTML, but when everything is said and done, the receiving side sees the garbage:

enter image description here

This code is simple, I have a subclass of ActionMailer (which uses UTF-8 by default) to send an HTML letter encoding UTF-8 content in the layout:

email.html.erb layout file:

 <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="X-UA-Compatible" content="chrome=1,IE=9" /> <meta charset="utf-8"> <%= stylesheet_link_tag "application", :media => "all" %> </head> <body class='email'> <%= yield %> </body> </html> 

The content uses the same views that display the web page, with an important line:

 <p><%= simple_format strip_tags(comment.text) %></p> 

I tried many permutations of force_encoding , encode , applying redundant UTF-8 encoding to a subclass of ActionMailer , the top of the view file and about a dozen other things, but nothing works.

Important HTML code being considered raw html message via Apple Mail:

 Website =EF=BF=BD=EF=BF=BD=EF=BF=BD=EF=BF=BD=EF=BF=BD=EF=BF=BD=EF=BF=BD=EF=BF=BD=EF=BF=BD Facebook 

If you look at the raw message (linked above), this does not happen in the text message, but only in the HTML file.

TL DR

ActionMailer replaces what should be a UTF-8 token character: 0xE2 0x80 0xA2
with garbage: 0xEF 0xBF 0xBD 0xEF 0xBF 0xBD 0xEF 0xBF 0xBD 0xEF 0xBF 0xBD 0xEF 0xBF 0xBD 0xEF 0xBF 0xBD 0xEF 0xBF 0xBD 0xEF 0xBF 0xBD 0xEF 0xBF 0xBD 0xEF 0xBF 0xBD

How can i fix this?

+6
source share
1 answer

Check if you use stone to embed CSS like premailer-rails3 .

According to this post , it could damage UTF-8 characters in your ActionMailer emails.

Try either removing or replacing the gem with something like the actionmailer_inline_css .

+2
source

All Articles