简体   繁体   English

使用Python和imaplib的GMail-在“所有邮件”中搜索

[英]GMail with Python and imaplib - searching in “All mail”

I'm trying to build an email client using Python and imaplib. 我正在尝试使用Python和imaplib构建电子邮件客户端。

Currently my problem is: I want to do searches on the folder that contains all the email. 当前我的问题是:我想在包含所有电子邮件的文件夹上进行搜索。 Usually called "[GMail]/All email". 通常称为“ [GMail] /所有电子邮件”。

My problem is, my GMail is natively Portuguese. 我的问题是,我的GMail是葡萄牙语。 Therefore, in my case, doing a select on "[Gmail]/Todo o correio", which is the portuguese equivalent, works, but selecting "[Gmail/All email" does not work. 因此,在我的情况下,选择与葡萄牙语等效的“ [Gmail] / Todo o correio”是可行的,但是选择“ [Gmail /所有电子邮件”则不起作用。

This is a problem, because naturally I want a universal way to access my "All mail" folder in any Gmail account regardless of country. 这是一个问题,因为自然地,我想要一种通用的方式来访问任何Gmail帐户中的“所有邮件”文件夹,而不受国家/地区的影响。

And of course I could follow a naive solution such as doing the search on all folders and returning the aggregate results, but I definitely do not want to go down that path. 当然,我可以遵循一个简单的解决方案,例如在所有文件夹上进行搜索并返回汇总结果,但是我绝对不想走那条路。

Is the a universal fix for this? 是通用解决方案吗? Thank you 谢谢

Found a solution shortly after. 不久后找到了解决方案。 Will mention for convenience. 为了方便将提及。

When you run mail_instance.list() you obtain a list of folders, such as: 运行mail_instance.list()时,您将获得一个文件夹列表,例如:

(\Noselect \HasChildren) "/" "[Gmail]"
(\HasChildren \HasNoChildren \Trash) "/" "[Gmail]/Caixote do Lixo"
(\HasNoChildren \Flagged) "/" "[Gmail]/Com estrela"
(\HasChildren \HasNoChildren \Sent) "/" "[Gmail]/Correio enviado"
(\HasChildren \HasNoChildren \Important) "/" "[Gmail]/Importante"
(\HasChildren \HasNoChildren \Drafts) "/" "[Gmail]/Rascunhos"
(\HasNoChildren \Junk) "/" "[Gmail]/Spam"
(\HasChildren \HasNoChildren \All) "/" "[Gmail]/Todo o correio"

(these are in Portuguese) (这些都是葡萄牙语)

The name of the "all mail" folder always contains the tag "\\All" in the string, so just do a search for "\\All" in all folder names that are returned by .list(). “所有邮件”文件夹的名称始终在字符串中包含标签“ \\ All”,因此只需在.list()返回的所有文件夹名称中搜索“ \\ All”。

Then just extract the substring from the index of "[Gmail]" to the index of the first double quote after that, and you obtain the "all mail" folder name. 然后,只需将子字符串从“ [Gmail]”的索引提取到此后的第一个双引号的索引,即可获得“所有邮件”文件夹的名称。

See this answer: https://stackoverflow.com/a/3846248/2131094 看到这个答案: https : //stackoverflow.com/a/3846248/2131094

"Google and Apple developed a special IMAP command XLIST to address this issue." “谷歌和苹果公司开发了一种特殊的IMAP命令XLIST来解决此问题。”

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

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