Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionMailer HTML Encoding Hell - Special Chars Replaced With Garbage

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

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

I am attempting to send this via HTML email, but when all is said and done, the receiving end sees garbage:

enter image description here

The code behind this is simple, I have an ActionMailer subclass (which uses UTF-8 by default) setup to send an HTML email with UTF-8 content encoding 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 render the webpage, with the important line being:

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

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

The important HTML, as viewed by the raw html message through Apple Mail is:

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 take a look at the raw message (linked above), this does not occur on the text-only message, only the HTML one.

TL;DR

ActionMailer is replacing what should be a UTF-8 bullet 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?

like image 829
coneybeare Avatar asked Dec 18 '12 19:12

coneybeare


1 Answers

Check if you are using a gem for inlining CSS, like premailer-rails3.

According to this post, this may corrupt UTF-8 characters in your ActionMailer emails.

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

like image 132
mccannf Avatar answered Oct 14 '22 03:10

mccannf