Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle UTF-8 email headers (like Subject:) using Ruby?

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.

like image 913
Luke Francl Avatar asked Dec 04 '08 19:12

Luke Francl


2 Answers

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.

like image 94
Tomalak Avatar answered Sep 19 '22 19:09

Tomalak


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!

like image 44
Luke Francl Avatar answered Sep 22 '22 19:09

Luke Francl