2012-06-14 10 views
9

मैं Ruby mail gem के साथ ईमेल स्ट्रिंग को पार्स करने का प्रयास कर रहा हूं, और मेरे पास चरित्र एन्कोडिंग के साथ एक समय का शैतान है। निम्नलिखित ईमेल लें:रूबी 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 

हालांकि, मैं एक समय वास्तव में एक्सेंट चिन्ह के माध्यम से आने के लिए प्राप्त करने की एक शैतान हो रही है।

body.encoding # => <Encoding:ASCII-8BIT> 
body.encode("UTF-8") # => Encoding::UndefinedConversionError: "\xA0" from ASCII-8BIT to UTF-8 

किसी को भी किसी भी सुझाव पर है: मैं इस कोशिश अंत में

This reply has accents:\xA0R\xE9sum\xE9\r\n>\r\n> --------- Reply Above This Line ------------ 

,: यहाँ मैं क्या करने की कोशिश की है या नहीं:

message = Mail.new(email_string) 
body = message.body.decoded 

मुझे एक स्ट्रिंग है कि इस तरह शुरू होता है हो जाता है यही कारण है कि इस से निपटने के लिए कैसे? मुझे पूरा यकीन है कि इसे ईमेल में "charset = ISO-8859-1" सेटिंग के साथ करना है, लेकिन मुझे यकीन नहीं है कि इसका उपयोग कैसे करें, या यदि मेल मणि का उपयोग करके आसानी से निकालने का कोई तरीका है।

उत्तर

17

एक सा खेलने के बाद, मैंने पाया इस:

body.decoded.force_encoding("ISO-8859-1").encode("UTF-8") # => "This reply has accents: Résumé..." 
message.parts.map { |part| part.decoded.force_encoding("ISO-8859-1").encode(part.charset) } # multi-part 

तुम इतनी तरह संदेश से चारसेट निकाल सकते हैं। के रूप में निम्नलिखित मुसीबत पैदा कर सकता है

message.charset #=> for simple, non-multipart 
message.parts.map { |part| part.charset } #=> for multipart, each part can have its own charset 

गैर बहुखण्डीय से सावधान रहें,:

body.charset #=> returns "US-ASCII" which is WRONG! 
body.force_encoding(body.charset).encode("UTF-8") #=> Conversion error... 

body.force_encoding(message.charset).encode("UTF-8") #=> Correct conversion :) 
+1

बहुत बढ़िया। इसके लिए देख रहे थे। ऐसा करने में समाप्त: body = message.text_part.encode ('UTF-8', message.text_part.charset,: अवैध =>: प्रतिस्थापित करें:: undef =>: प्रतिस्थापित करें) –

+0

बहुत बढ़िया ...... धन्यवाद एक ट्यून ... – Jyothu

+0

कुछ हिस्सों में एक शून्य वर्णमाला प्रतीत होता है। मुझे यकीन नहीं है कि अभी तक उन्हें कैसे संभालना है। –

0

यह मेरे लिए काम नहीं किया है, इसलिए सोचा कि मैं समाधान मैं मामले में मिल गया ऊपर रहना चाहते हैं यह किसी की मदद करता है ...

मूल रूप से एन्कोडिंग डिफ़ॉल्ट जोड़ना और आउटपुट को समझदार तारों में ट्विक करना पड़ा। https://stackoverflow.com/a/26604049/2386548