Python IMAP 搜索从或到指定的电子邮件地址

标签 python imap imaplib

我在 Gmail 的 SMTP 服务器上使用它,我想通过 IMAP 搜索发送到某个地址或从某个地址接收的电子邮件。

这是我的:

mail = imaplib.IMAP4_SSL('imap.gmail.com')

mail.login('user', 'pass')
mail.list()
mail.select("[Gmail]/All Mail")

status, email_ids = mail.search(None, 'TO "tech163@fusionswift.com" OR FROM "tech163@fusionswift.com"')

错误的最后一行是:imaplib.error: SEARCH command error: BAD ['Could not parse command']

不确定我应该如何在 python 的 imaplib 中执行那种 OR 语句。如果有人能快速解释问题所在或为我指出正确的方向,将不胜感激。

最佳答案

您收到的错误是由服务器生成的,因为它无法正确解析搜索查询。为了生成有效的查询,请遵循 RFC 3501 , 在第 49 页中详细解释了结构。

例如,您的正确搜索字符串应该是:

'(OR (TO "tech163@fusionswift.com") (FROM "tech163@fusionswift.com"))'

关于Python IMAP 搜索从或到指定的电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10563218/

相关文章:

python - 打包许多小部件时的 Tkinter 性能

python - 如何访问 Django 中查询集的特定字段?

java mail Store.connect 挂起 - 没有超时

gmail - 本地化的 Gmail IMAP 文件夹

PHP imap 通过代理

python - IMAPClient 和 BODY[HEADER.FIELDS (FROM)] 字段

python imaplib UID命令错误: BAD [b'Could not parse command']

python - 添加一个以 "h"开头的参数

python - 类型错误 : can't concat bytes to IMAP4_SSL

python - 在 Python 包中应该如何使用日志记录?