views:

2819

answers:

2

I'm an email n00b but I am working on an application that sends HTML email with Unicode characters (as my friend noted "enjoy encoding hell").

The Subject: header comes from user input and therefore may contain Unicode characters. Some mail clients (like GMail and Outlook 2007) are OK with this, but from my reading it seems the right way to do this is to use MIME Encoded-Word encoding for the headers.

I cannot find a Ruby library to do this. Is there one?

Also, is there a header to add that will tell mail clients to use UTF-8 when displaying the message? We are sending multipart email so our Content-Type is multipart/mixed. Apple Mail.app in particular is not using the right encoding, even though it's specified in the individual parts as being UTF-8.

+1  A: 

Your can optionally do the same using Base64 encoding:

require "base64"

value  = Base64.encode64("Your UTF-8 string")
header = "=?UTF-8?B?" + value + "?="

Note the "B", that marks Base64 encoded payload, as opposed to the "Q" which marks "Q-encoded" payload. The latter could be faked by URL-encoding the string and replacing all "%" characters with "=".

By "faking" I mean this: It would produce a valid result, but maybe more characters are encoded than would be necessary. The spec allows for encoding every character there is with "=" + ByteCodeAsHex, it merely impairs human readability of the raw headers. UrlEncode is + .gsub(/%/, "=") not a bad compromise when nothing else is available.

Tomalak
Thanks! I started out by using Base64 but I was worried about spam filters flagging that, so I switched to quoted printable. I found a method to do that in ActionMailer::Quoting. Looks like it works too!
Luke Francl
I don't think spam filters would flag this, this is a valid header value.
Tomalak
Yeah, I don't have any proof of that. I thought spammers might try it to get around naive content filters.
Luke Francl
+2  A: 

Ahah! ActionMailer::Quoting has a quoted_printable method.

So here's what I did:

def my_email(foo)
  ...
  @subject = quoted_printable(foo.some_subject_with_accented_chars, 'utf-8')
  ...
end

Doing this convinced Mail.app to display the rest of the email using UTF-8. Now to test the rest!

Luke Francl
Should be fine as well.
Tomalak