私はRuby mail gemで電子メール文字列を解析しようとしています。私は文字エンコーディングの悪魔を抱えています。Ruby 1.9.3とメール宝石を使った文字エンコーディング
MIME-Version: 1.0
Sender: [email protected]
Received: by 10.142.239.17 with HTTP; Thu, 14 Jun 2012 06:00:18 -0700 (PDT)
Date: Thu, 14 Jun 2012 09:00:18 -0400
Delivered-To: [email protected]
X-Google-Sender-Auth: MxfFrMybNjBoBt4O4GwAn9cMsko
Message-ID: <[email protected]om>
Subject: Re: [Lorem Ipsum] Foo updated the forum topic 'Reply by email test'
From: Foo Bar <[email protected]>
To: Foo <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
This email has accents:=A0R=E9sum=E9
>
> --------- Reply Above This Line ------------
>
> Email parsing with accents: R=E9sum=E9
>
> Click here to view this post in your browser
電子メールの本文を、適切にエンコードする場合、次のようになります。次のメールを取る
This reply has accents: Résumé
>
> --------- Reply Above This Line ------------
>
> Email parsing with accents: Résumé
>
> Click here to view this post in your browser
しかし、私は実際に通ってくるためにアクセント記号を取得し、時間の悪魔を抱えています。ここで私が試したものです:
私にこのように始まる文字列を取得しますmessage = Mail.new(email_string)
body = message.body.decoded
:
This reply has accents:\xA0R\xE9sum\xE9\r\n>\r\n> --------- Reply Above This Line ------------
は最後に、私はこれを試してみてください。
body.encoding # => <Encoding:ASCII-8BIT>
body.encode("UTF-8") # => Encoding::UndefinedConversionError: "\xA0" from ASCII-8BIT to UTF-8
誰でも上の任意の提案を持っていますこれに対処する方法は?私はそれが電子メールの "charset = ISO-8859-1"設定と関係しているとはかなり確信していますが、その使い方がわからない、またはメール宝石を使って簡単に抽出する方法があるとします。
驚くばかりです。これを探していた。これを終えた:body = message.text_part.encode( 'UTF-8'、message.text_part.charset、:無効=>:置換、:undef =>:置換) –
素晴らしい...ありがとう... – Jyothu
一部の部品には、文字セットがないようです。私はそれらをどう扱うかまだ分かりません。 –