为什么我们要在 imaplib 搜索方法中写 None

why we should write None in imaplib search method

代码如下:

box = imaplib.IMAP4_SSL(HOST,PORT)
box.login(sender,password)
box.select("INBOX")
data = box.search(None, "all")

我不明白一件事,为什么我必须在搜索方法中写None?我找到的关于使用imaplib的每个教程都说我们应该写None但是没有人解释why.can 有人给我一些相关信息吗?

这就是有时阅读文档很重要的地方,因为它会告诉您的不仅仅是教程:

https://docs.python.org/3.8/library/imaplib.html#imaplib.IMAP4.search

来自文档 - 搜索方法的签名是:

IMAP4.search(charset, criterion[, ...])

Search mailbox for matching messages. charset may be None, in which case no CHARSET will be specified in the request to the server. The IMAP protocol requires that at least one criterion be specified; an exception will be raised when the server returns an error. charset must be None if the UTF8=ACCEPT capability was enabled using the enable() command.

所以,第一个参数将是 None,如果:

  • 您不关心搜索过程中使用的字符集或
  • 您在启用邮箱时指定了 UTF8=ACCEPT

由于您没有使用 enable,第一个条件适用,因此您的代码片段在收件箱中搜索所有带有任何字符集的主题 'all' 的电子邮件。