选择邮箱“已发送邮件”或“所有邮件”在 Ruby Net::IMAP 中
我正在尝试在 Ruby 中使用 Net::IMAP 来搜索我发送的所有邮件,但在选择 INBOX 以外的任何邮件时遇到问题。
imap.select('INBOX')
工作正常,但
imap.select('Mail/sent-mail')
如 Net::IMAP 文档所示,给我“未知邮箱”。
顺便说一句,这是与 gmail 一起使用的。
我还尝试将“in”、“anywhere”添加到我的 imap.search() 中,但没有解析。
当前代码:
imap.select('INBOX')
now = Time.now.localtime - 1209600 #two weeks
since = now.day.to_s() + "-" + Date::MONTHNAMES[now.month] + "-" + now.year.to_s()
puts "since"
puts since
begin
mail_ids = imap.search(["FROM", "me", "SINCE", since])
mail_ids.each do |id|
text = imap.fetch(id, 'BODY[HEADER.FIELDS (SUBJECT)]').to_s.split("{").second.chop
puts text
end
end
I'm trying to use Net::IMAP in Ruby to search all mail sent by me, but I'm having trouble selecting anything other than INBOX.
imap.select('INBOX')
works fine, but
imap.select('Mail/sent-mail')
as shown on the Net::IMAP documentation gives me "Unknown Mailbox".
Incidentally, this is to be used with gmail.
I also tried adding "in", "anywhere" to my imap.search(), but that didn't parse.
Current code:
imap.select('INBOX')
now = Time.now.localtime - 1209600 #two weeks
since = now.day.to_s() + "-" + Date::MONTHNAMES[now.month] + "-" + now.year.to_s()
puts "since"
puts since
begin
mail_ids = imap.search(["FROM", "me", "SINCE", since])
mail_ids.each do |id|
text = imap.fetch(id, 'BODY[HEADER.FIELDS (SUBJECT)]').to_s.split("{").second.chop
puts text
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
“已发送邮件”文件夹因提供商而异。 Gmail 的“已发送邮件”文件夹名为“
[Gmail]/Sent Mail
”。选择它即可。仅供参考,Gmail 的系统文件夹如下:
INBOX
[Gmail]/All Mail
[Gmail]/Drafts
[Gmail]/Sent邮件
[Gmail]/垃圾邮件
[Gmail]/加星标
[Gmail]/垃圾邮件
The "sent mail" folder will differ from provider to provider. Gmail's "sent mail" folder is named "
[Gmail]/Sent Mail
". Select that instead and it'll work.FYI, Gmail's system folders are the following:
INBOX
[Gmail]/All Mail
[Gmail]/Drafts
[Gmail]/Sent Mail
[Gmail]/Spam
[Gmail]/Starred
[Gmail]/Trash
您可以通过以下方式查找所有文件夹的名称:
Gmail 文件夹名称将根据用户选择的语言而变化。例如,在西班牙语中:
“[Gmail]/All”邮件将是“[Gmail]/Todos”
You can find the names of all folders with:
The Gmail folders name's will change depending on the user selected language. So in Spanish for example:
"[Gmail]/All" Mail will be "[Gmail]/Todos"
我发现以下内容很有帮助(ruby 2.0.0-p195)
I found the following to be helpful (ruby 2.0.0-p195)
不要使用
LIST "" *
。你们中的许多人最终会拥有数千个邮箱。使用LIST "" %
。如果您只对children/subfolders
感兴趣,您可以对imap.list '' 执行类似
imap.list ''、'%/%'
等操作', '%/%/%'仅列出父文件夹,深度 1。
列出子文件夹。深度2。
Don't use
LIST "" *
. You many end up with thousands of mailboxes. UseLIST "" %
. If you are only interested inchildren/subfolders
, you can do something likeimap.list '', '%/%'
and so onimap.list '', '%/%/%'
lists parent(s) folder only, depth 1.
list children . depth 2.