How to handle UTF-8 email headers (e.g. Subject :) using Ruby?

I am an e-mail n00b, but I am working on an application that sends an HTML letter with Unicode characters (as my friend said “like hell coding”).

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

I can not find the Ruby library for this. There is one?

Also, is there a heading to add that tells email clients to use UTF-8 when displaying a message? We send multi-page emails so that our Content-Type multipart/mixed . Apple Mail.app, in particular, does not use the correct encoding, even if it is listed in some parts as UTF-8.

+6
ruby email ruby-on-rails unicode utf-8
source share
3 answers

Ahaha! ActionMailer::Quoting has a quoted_printable method.

So here is what I did:

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

When doing this, make sure Mail.app displays the rest of the email using UTF-8. Now to check the rest!

+5
source share

Perhaps you can do the same using Base64 encoding:

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

Note the “B,” which marks the Base64 encoded payload, as opposed to the “Q,” which marks the “Q-encoded” payload. The latter may be due to the URL encoding of the string and replacing all the characters "%" with "=".

By "falsification" I mean this: it will produce a valid result, but perhaps more characters are encoded than would be necessary. The specification allows each character to be encoded with "=" + ByteCodeAsHex , this simply degrades the readability of raw headers. UrlEncode + .gsub(/%/, "=") not a bad compromise when nothing is available.

+9
source share

Since none of the answers speaks of the whole message with pure Ruby, here it is.

 Net::SMTP.start("localhost") do |smtp| smtp.open_message_stream opts[:sender_address], opts[:receiver_address] do |f| f.puts "Content-type: text/plain; charset=UTF-8" f.puts from f.puts to f.puts subject f.puts message end end 

Here you open a connection with localhost. Using an external SMTP server is also possible, see the net / smtp documentation.

The character set of the first line used in the message. The remaining lines are variables defined separately:

from is an address in the form From: Name here < address@here.fi > . If a name is not required, only the address can be specified, for example From: address@here.fi .

uses the same syntax, with the exception From: changed to To:

the subject is in the form of Subject: subject here. For UTF-8, it must be encoded in Base64 to display correctly for clients.

 subject = "Subject: =?UTF-8?B?" + Base64.strict_encode64(subject) + "?=" 

The message is a text-to-text message encoded in UTF-8 without a prefix. net / smtp will take care of creating proper mail.

+3
source share

All Articles