Skip to content

Instantly share code, notes, and snippets.

@gudleik
Created September 14, 2010 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gudleik/578879 to your computer and use it in GitHub Desktop.
Save gudleik/578879 to your computer and use it in GitHub Desktop.
Using Ruby-1.9.2-p0 and mail-2.2.6.1
ruby-1.9.2-p0> mail = Mail.new <<-EOT
X-Original-To: support@lvh.me
Return-Path: <foobar@example.org>
Received: from smarthost01.example.org (smarthost01.example.org [91.186.91.50])
by drift.example.org (Postfix) with ESMTP id 9E2792C3418
for <test@chakra.example.org>; Tue, 14 Sep 2010 12:11:08 +0200 (CEST)
From: Foobar <foobar@example.org>
To: "test@foo.example.org" <test@foo.example.org>
Content-Class: urn:content-classes:message
Date: Tue, 14 Sep 2010 12:10:59 +0200
Subject: Test email
Message-ID: <F06D5594-BFC7-4A03-A366-478B629C4365@example.org>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
acceptlanguage: en-US
Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
This is an email with Windows-1252 charset. The body is encoded =
using quoted-printable.
Here's some quoted text:
Systemingeni=F8r
Foo Inc. p=E5tar seg ikke ansvar for ufull=ADstendig =F8l
EOT
ruby-1.9.2-p0> mail.charset
=> "Windows-1252"
ruby-1.9.2-p0> mail.body.charset
=> "US-ASCII"
ruby-1.9.2-p0 > mail.body.encoding
=> "quoted-printable"
ruby-1.9.2-p0 > mail.body.to_s.encoding
=> #<Encoding:US-ASCII>
# To get the result I'm looking for, I have to do this:
ruby-1.9.2-p0> result = mail.body.to_s.encode(mail.charset).encode("UTF-8")
=> "This is an email with Windows-1252 charset. The body is encoded using quoted-printable.\r\n\r\nHere's some quoted text:\r\nSystemingeniør\r\n\r\nFoo Inc. påtar seg ikke ansvar for ufull­stendig øl"
# This doesn't work because mail.body.encoding is US-ASCII and not Windows-1252(?)
ruby-1.9.2-p0 > mail.body.to_s.encode("utf-8") == result
=> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment