简体   繁体   English

如何解码IMAP提取结果?

[英]How to decode a IMAP fetch result?

Hi I am using the ruby stdlib net/imap fetch method. 嗨,我正在使用ruby stdlib net/imap fetch方法。

I am calling this way: 我这样打电话:

headers = imap.fetch range, "BODY[header]"

Everything is fine, but I get in response a Struct, with a attr key with the following string: 一切都很好,但作为响应,我得到了一个Struct,带有attr键和以下字符串:

"Return-Path: <blabl@bla.biz>\r\nDelivered-To: try.com-prova.prova@try.com\r\nReceived: (qmail 21596 invoked by uid 106); 30 Oct 2014 11:11:16 -0000\r\nX-Originating-IP: 109.168.113.152\r\nX-Qmail-Scanner-Diagnostics: from mail.zum.biz by mx02eeh (envelope-from <zum@zum.biz>, uid 89) with qmail-scanner-2.11st \r\n (clamdscan: 0.98.1/19557. spamassassin: 3.3.2. perlscan: 2.11st.  \r\n Clear:RC:1(109.168.113.152):. \r\n Processed in 0.664894 secs); 30 Oct 2014 11:11:16 -0000\r\nReceived: from mail.zum.biz (109.168.113.152)\r\n  by 0 with SMTP; 30 Oct 2014 11:11:15 -0000\r\nReceived: (qmail 28197 invoked by uid 108); 30 Oct 2014 11:11:15 -0000\r\nX-Originating-IP: 88.149.230.97\r\nX-Qmail-Scanner-Diagnostics: from 88-149-230-97.v4.ngi.it (zum@zum.biz@88-149-230-97.v4.ngi.it) by mail-zum.cbsolt.net (envelope-from <alessio@zum.biz>, uid 89) with qmail-scanner-2.11st \r\n (clamdscan: 0.98.4/19501. spamassassin: 3.4.0. perlscan: 2.11st.  \r\n Clear:RC:1(88.149.230.97):. \r\n Processed in 0.481596 secs); 30 Oct 2014 11:11:15 -0000\r\nReceived: from 88-149-230-97.v4.ngi.it (HELO ?192.168.5.104?) (zum@zum.biz@88.149.230.97)\r\n  by 0 with ESMTPA; 30 Oct 2014 11:11:00 -0000\r\nMessage-ID: <54521CC1.5030105@zum.biz>\r\nDate: Thu, 30 Oct 2014 12:10:57 +0100\r\nFrom: CBS - zum zum <zum@zum.biz>\r\nOrganization: zum Business Solutions\r\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0\r\nMIME-Version: 1.0\r\nTo: prova.prova@try.com\r\nSubject: Fwd: Re: Nuovo sito web, Webmail SSL, Sicurezza ed Beta Archivio\r\n Email\r\nReferences: <30D51861-7016-423D-BD10-963162547964@gmail.com> <CA94F654-03A9-4728-8FE8-C04EE09D195E@gmail.com> <AC91AEE8-A0F1-44DA-A7BF-7032D41902EC@gmail.com> <0CE556AF-1FDD-4A3F-8B11-009A0D251A7E@gmail.com>\r\nIn-Reply-To: <0CE556AF-1FDD-4A3F-8B11-009A0D251A7E@gmail.com>\r\nX-Forwarded-Message-Id: <30D51861-7016-423D-BD10-963162547964@gmail.com> <CA94F654-03A9-4728-8FE8-C04EE09D195E@gmail.com> <AC91AEE8-A0F1-44DA-A7BF-7032D41902EC@gmail.com> <0CE556AF-1FDD-4A3F-8B11-009A0D251A7E@gmail.com>\r\nContent-Type: multipart/mixed;\r\n boundary=\"------------050205030209030608020806\"\r\n\r\n"

This is a string and it is pretty unusable. 这是一个字符串,非常无法使用。 I want to know if there is a way to decode it and map it to an hash or an object. 我想知道是否有一种方法可以将其解码并将其映射到哈希或对象。

Or if I need to do manually, any suggestion? 或者,如果我需要手动操作,有什么建议吗?

attr = "Return-Path..."
attr.split(/\r\n(?=\S)/).map { |e| e.split /:\s*/, 2 }.to_h
#⇒ {
#                 "Content-Type" => "multipart/mixed;\r\n boundary=\"------------050205030209030608020806\"\r\n\r\n",
#                         "Date" => "Thu, 30 Oct 2014 12:10:57 +0100",
#                 "Delivered-To" => "try.com-prova.prova@try.com",
#                         "From" => "CBS - zum zum <zum@zum.biz>",
#                  "In-Reply-To" => "<0CE556AF-1FDD-4A3F-8B11-009A0D251A7E@gmail.com>",
#                 "MIME-Version" => "1.0",
#                   "Message-ID" => "<54521CC1.5030105@zum.biz>",
#                 "Organization" => "zum Business Solutions",
#                     "Received" => "from 88-149-230-97.v4.ngi.it (HELO ?192.168.5.104?) (zum@zum.biz@88.149.230.97)\r\n  by 0 with ESMTPA; 30 Oct 2014 11:11:00 -0000",
#                   "References" => "<30D51861-7016-423D-BD10-963162547964@gmail.com> <CA94F654-03A9-4728-8FE8-C04EE09D195E@gmail.com> <AC91AEE8-A0F1-44DA-A7BF-7032D41902EC@gmail.com> <0CE556AF-1FDD-4A3F-8B11-009A0D251A7E@gmail.com>",
#                  "Return-Path" => "<blabl@bla.biz>",
#                      "Subject" => "Fwd: Re: Nuovo sito web, Webmail SSL, Sicurezza ed Beta Archivio\r\n Email",
#                           "To" => "prova.prova@try.com",
#                   "User-Agent" => "Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0",
#       "X-Forwarded-Message-Id" => "<30D51861-7016-423D-BD10-963162547964@gmail.com> <CA94F654-03A9-4728-8FE8-C04EE09D195E@gmail.com> <AC91AEE8-A0F1-44DA-A7BF-7032D41902EC@gmail.com> <0CE556AF-1FDD-4A3F-8B11-009A0D251A7E@gmail.com>",
#             "X-Originating-IP" => "88.149.230.97",
#  "X-Qmail-Scanner-Diagnostics" => "from 88-149-230-97.v4.ngi.it (zum@zum.biz@88-149-230-97.v4.ngi.it) by mail-zum.cbsolt.net (envelope-from <alessio@zum.biz>, uid 89) with qmail-scanner-2.11st \r\n (clamdscan: 0.98.4/19501. spamassassin: 3.4.0. perlscan: 2.11st.  \r\n Clear:RC:1(88.149.230.97):. \r\n Processed in 0.481596 secs); 30 Oct 2014 11:11:15 -0000"
# }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM