您好我正在使用ruby stdlib net/imap
fetch
方法。
我这样称呼:
headers = imap.fetch range, "BODY[header]"
一切都很好,但我得到一个结构,带有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"
这是一个字符串,它几乎无法使用。我想知道是否有解码方法并将其映射到散列或对象。
或者如果我需要手动做任何建议?
答案 0 :(得分:1)
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"
# }