Warm tip: This article is reproduced from serverfault.com, please click

email-Python imaplib 无法选择()自定义 Gmail 标签

(email - Python imaplib can't select() custom gmail labels)

发布于 2021-01-12 21:13:06

我有一个机器人,我在 python 中使用 imaplib 编写,从 gmail 获取电子邮件并从中输出一些有用的数据。不过,我在选择收件箱时遇到了障碍;现有的分类系统使用自定义标签来区分来自不同客户的电子邮件。我已经在我的测试电子邮件中部分复制了这个系统,但是 imaplib.select() 会抛出一个带有自定义标签的“imaplib.IMAP4.error: SELECT command error: BAD [b'Could not parse command']”。附上截图我的机器人对默认的 gmail 文件夹没有问题,获取收件箱或 [Gmail]/垃圾邮件。在这种情况下,它会在处理我尚未修复的完全不同的问题的代码中稍后遇到错误不过,重点是 imaplib.select() 对默认收件箱是成功的,而不仅仅是自定义标签。

我的代码的工作方式是处理所有可用的收件箱,将其与用户输入的名称进行比较,如果匹配,则保存名称并将布尔值设置为 true 以表示它找到了匹配项。然后它检查,如果有匹配(用户输入的收件箱存在),它会继续,否则它会抛出一条错误消息并重置。然后它尝试选择用户输入的收件箱。

我已经验证程序保存收件箱名称的变量与 imap.list() 命令中列出的名称相匹配。我不知道是什么问题。

我可以通过遍历所有邮件来找到我正在寻找的电子邮件来绕过这个过程,但由于我将使用的帐户上的电子邮件数量庞大,使用现有的分类系统效率更高。

任何帮助表示赞赏!

编辑:请求后附上代码。感谢告诉我这样做的人。

'''
Fetches emails from the specified inbox and outputs them to a popup
'''
def fetchEmails(self):
    #create an imap object. Must be local otherwise we can only establish a single connection
    #imap states are kinda bad
    imap = imaplib.IMAP4_SSL(host="imap.gmail.com", port="993")
    #Login and fetch a list of available inboxes
    imap.login(username.get(), password.get())
    type, inboxList = imap.list()
        
    #Set a reference boolean and iterate through the list
    inboxNameExists = False
    for i in inboxList:
        #Finds the name of the inbox
        name = self.inboxNameParser(i.decode())
            
        #If the given inbox name is encountered, set its existence to true and break
        if name.casefold().__eq__(inboxName.get().casefold()):
            inboxNameExists = True
            break
        
        #If the inbox name does not exist, break and give error message
        if inboxNameExists != True:
            self.logout(imap)
            tk.messagebox.showerror("Disconnected!", "That Inbox does not exist.")
            return
        
    '''
    If/else to correctly feed the imap.select() method the inbox name
    Apparently inboxes containing spaces require quoations before and after
        
    Selects the inbox and pushes it to a variable
    two actually but the first is unnecessary(?)
    imap is weird
    '''
    if(name.count(" ") > 0):
        status, messages = imap.select("\"" + name + "\"")
    else:
        status, messages = imap.select(name);
    
    #Int containing total number of emails in inbox
    messages = int(messages[0])
        
    #If there are no messages disconnect and show an infobox
    if messages == 0:
        self.logout(imap)
        tk.messagebox.showinfo("Disconnected!", "The inbox is empty.")
        
    self.mailboxLoop(imap, messages)

与朋友讨论了几个小时后解决了这个问题。事实证明,问题是 imap.select() 想要在邮箱名称周围加上引号,如果它包含空格。所以 imap.select("INBOX") 很好,但有空格你需要 imap.select("\"" + "Label Name" + "\"")

你可以在我与最后一个 if/else 语句一起发布的代码中看到这一点。

Questioner
JaySay
Viewed
0
JaySay 2021-01-14 02:01:50

Python imaplib 要求邮箱名称用撇号包围空格。所以 imap.select("INBOX") 很好,但如果有空格,你需要 imap.select("\"" + "Label Name" + "\"")。