选择邮箱“已发送邮件”或“所有邮件”在 Ruby Net::IMAP 中

发布于 2024-10-20 11:47:37 字数 722 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清风无影 2024-10-27 11:47:37

“已发送邮件”文件夹因提供商而异。 Gmail 的“已发送邮件”文件夹名为“[Gmail]/Sent Mail”。选择它即可。

imap.select('[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.

imap.select('[Gmail]/Sent Mail')

FYI, Gmail's system folders are the following:

  • INBOX
  • [Gmail]/All Mail
  • [Gmail]/Drafts
  • [Gmail]/Sent Mail
  • [Gmail]/Spam
  • [Gmail]/Starred
  • [Gmail]/Trash
筱武穆 2024-10-27 11:47:37

您可以通过以下方式查找所有文件夹的名称:

imap.list('*', '*') 

Gmail 文件夹名称将根据用户选择的语言而变化。例如,在西班牙语中:

“[Gmail]/All”邮件将是“[Gmail]/Todos”

You can find the names of all folders with:

imap.list('*', '*') 

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"

仅一夜美梦 2024-10-27 11:47:37

我发现以下内容很有帮助(ruby 2.0.0-p195)

# list all folders
imap.list '', '%'

I found the following to be helpful (ruby 2.0.0-p195)

# list all folders
imap.list '', '%'
提赋 2024-10-27 11:47:37

不要使用LIST "" *。你们中的许多人最终会拥有数千个邮箱。使用 LIST "" % 。如果您只对 children/subfolders 感兴趣,您可以对 imap.list '' 执行类似 imap.list ''、'%/%' 等操作', '%/%/%'

仅列出父文件夹,深度 1。

C: RUBY0002 LIST "" "%"
S: * LIST (\HasNoChildren) "/" Calendar
S: * LIST (\HasNoChildren) "/" Contacts
S: * LIST (\HasNoChildren) "/" "Deleted Items"
S: * LIST (\HasNoChildren) "/" Drafts
S: * LIST (\Marked \HasChildren) "/" INBOX
S: * LIST (\HasNoChildren) "/" Journal
S: * LIST (\HasNoChildren) "/" "Junk E-Mail"
S: * LIST (\HasNoChildren) "/" Notes
S: * LIST (\HasNoChildren) "/" Outbox
S: * LIST (\HasNoChildren) "/" "Sent Items"
S: * LIST (\HasNoChildren) "/" Tasks
S: RUBY0002 OK LIST completed.

列出子文件夹。深度2。

C: RUBY0003 LIST "" "%/%"
S: * LIST (\HasNoChildren) "/" INBOX/subfolder
S: RUBY0003 OK LIST completed.
C: RUBY0004 SELECT INBOX/subfolder
S: * 2 EXISTS
S: * 0 RECENT
S: * FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
S: * OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
S: * OK [UIDVALIDITY 37286] UIDVALIDITY value
S: * OK [UIDNEXT 6] The next unique identifier value
S: RUBY0004 OK [READ-WRITE] SELECT completed.

Don't use LIST "" *. You many end up with thousands of mailboxes. Use LIST "" % . If you are only interested in children/subfolders, you can do something like imap.list '', '%/%' and so on imap.list '', '%/%/%'

lists parent(s) folder only, depth 1.

C: RUBY0002 LIST "" "%"
S: * LIST (\HasNoChildren) "/" Calendar
S: * LIST (\HasNoChildren) "/" Contacts
S: * LIST (\HasNoChildren) "/" "Deleted Items"
S: * LIST (\HasNoChildren) "/" Drafts
S: * LIST (\Marked \HasChildren) "/" INBOX
S: * LIST (\HasNoChildren) "/" Journal
S: * LIST (\HasNoChildren) "/" "Junk E-Mail"
S: * LIST (\HasNoChildren) "/" Notes
S: * LIST (\HasNoChildren) "/" Outbox
S: * LIST (\HasNoChildren) "/" "Sent Items"
S: * LIST (\HasNoChildren) "/" Tasks
S: RUBY0002 OK LIST completed.

list children . depth 2.

C: RUBY0003 LIST "" "%/%"
S: * LIST (\HasNoChildren) "/" INBOX/subfolder
S: RUBY0003 OK LIST completed.
C: RUBY0004 SELECT INBOX/subfolder
S: * 2 EXISTS
S: * 0 RECENT
S: * FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
S: * OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
S: * OK [UIDVALIDITY 37286] UIDVALIDITY value
S: * OK [UIDNEXT 6] The next unique identifier value
S: RUBY0004 OK [READ-WRITE] SELECT completed.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文