微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python imaplib 无法选择自定义 Gmail 标签

如何解决Python imaplib 无法选择自定义 Gmail 标签

我正在编写一个机器人,我在 python 中使用 imaplib 来从 gmail 获取电子邮件并从中输出一些有用的数据。不过,我在选择收件箱时遇到了障碍;现有的分类系统使用自定义标签来区分来自不同客户的电子邮件。我已经在我的测试电子邮件中部分复制了这个系统,但是 imaplib.select() 会抛出一个带有自定义标签的“imaplib.IMAP4.error: SELECT command error: BAD [b'Could not parse command']”。 Screenshot attatched 我的机器人对认的 Gmail 文件夹没有问题,可以获取收件箱或 [Gmail]/垃圾邮件在这种情况下,它会遇到 error later in the code that deals with completely different problem I have yet to fix。不过,关键是 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 语句一起发布的代码中看到这一点。

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。