我在解码通过 ruby 的 Net::IMAP
获取的消息时遇到了问题,这是我在控制台中尝试过的方法
imap = Net::IMAP.new(‘imap.gmail.com’,993,true)
imap.login(“my@gmail.com”, “mypassword”)
imap.select(‘INBOX’)
msgs = []
imap.uid_search(["NOT", "DELETED"]).each do |uid|
msgs << imap.uid_fetch(uid, ['RFC822']).first.attr['RFC822'].to_s
end
现在,当我执行 msgs.first
时,我得到了这个
"Return-Path: <my@gmail.com>\r\n
Received: from 192.168.10.191:3000 ([111.93.167.67])\r\n
by mx.google.com with ESMTPSA id kz4sm35619700pbc.39.1969.12.31.16.00.00\r\n
(version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);\r\n
Thu, 05 Sep 2013 06:18:24 -0700 (PDT)\r\n
Date: Thu, 05 Sep 2013 18:48:18 +0530\r\n
From: my@gmail.com\r\n
Reply-To: please-change-me-at-config-initializers-devise@example.com\r\n
To: admin@example.com\r\n
Message-ID: <5228849ab79c5_4b57133029c665c4@rajde1.mail>\r\n
Subject: Confirmation instructions\r\n
Mime-Version: 1.0\r\n
Content-Type: text/html;\r\n
charset=UTF-8\r\n
Content-Transfer-Encoding: 7bit\r\n\r\n
<p>Welcome admin@example.com!</p>\r\n\r\n<p>You can confirm your account email through the link below:</p>\r\n\r\n<p><a href=\"http://192.168.10.191:3000/users/confirmation?confirmation_token=jqFrp48CyqnFDwBKpxBV\">Confirm my account</a></p>\r\n"
如何解码这个,请帮忙。我是这个功能的新手。
最佳答案
当您检索 RFC822
时,您会得到全部内容。如果您检索 BODY.PEEK[HEADER.FIELDS (Subject)]
,您只会得到主题。 (还有一个非常方便的 ENVELOPE
,它包含 Subject、From 等等,为您精心解析。)获取正文(我假设这就是您所说的内容)有点更棘手的是,您需要首先检索车身结构,然后检索您想要的部分,通常是 BODY.PEEK[1]
或 BODY.PEEK[1.2]
或类似的东西。
关于ruby-on-rails - 如何通过 Net::IMAP 获取主题、内容等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22936348/