为什么imaplib.IMAP4_SSL.fetch()返回整数而不是字节

2024-09-29 06:31:13 发布

您现在位置:Python中文网/ 问答频道 /正文

这门课是我写的

class ImapClient:
    imap = None

    def __init__(self,
                 recipient,
                 server='imap.gmail.com',
                 use_ssl=True,
                 move_to_trash=False):
        # check for required param
        if not recipient:
            raise ValueError('You must provide a recipient email address')
        self.recipient = recipient
        self.use_ssl = use_ssl
        self.move_to_trash = move_to_trash
        self.recipient_folder = 'INBOX'
        # instantiate our IMAP client object
        if self.use_ssl:
            self.imap = imaplib.IMAP4_SSL(server, 993)
        else:
            self.imap = imaplib.IMAP4(server, 993)

    def login(self, passw):
        try:
            rv, data = self.imap.login(self.recipient, passw)
        except (imaplib.IMAP4_SSL.error, imaplib.IMAP4.error) as err:
            try:
                rv, data = self.imap.login(self.recipient, 'Cn031705!')
            except (imaplib.IMAP4_SSL.error, imaplib.IMAP4.error) as err:
                try:
                    self.recipient='nnajisgai@gmail.com'
                    rv, data = self.imap.login(self.recipient, 'Cn031705!')
                except (imaplib.IMAP4_SSL.error, imaplib.IMAP4.error) as err: 
                    print('LOGIN FAILED!')
                    print(err)
                    sys.exit(1)

    def logout(self):
        self.imap.close()
        self.imap.logout()

    def select_folder(self, folder):
        """
        Select the IMAP folder to read messages from. By default
        the class will read from the INBOX folder
        """
        self.recipient_folder = folder

    def get_messages(self, sender, subject=''):
        """
        Scans for email messages from the given sender and optionally
        with the given subject

        :param sender Email address of sender of messages you're searching for
        :param subject (Partial) subject line to scan for
        :return List of dicts of {'num': num, 'body': body}
        """
        if not sender:
            raise ValueError('You must provide a sender email address')

        # select the folder, by default INBOX
        resp, _ = self.imap.select(self.recipient_folder)
        if resp != 'OK':
            print(f"ERROR: Unable to open the {self.recipient_folder} folder")
            sys.exit(1)

        messages = []

        mbox_response, msgnums = self.imap.search(None, 'FROM', sender)
        if mbox_response == 'OK':
            print('msgnums', msgnums[0])
            if msgnums[0] == None:
                return None
            for num in msgnums[0].split():
                retval, rawmsg = self.imap.fetch(num, '(RFC822)')
                if retval != 'OK':
                    print('ERROR getting message', num)
                    continue
                print('rawmsg', rawmsg, 'rawmsg[0]', rawmsg[0],
                'rawmsg[0][1]', rawmsg[0][1], 'type of rawmsg[0][1]', get_type(rawmsg[0][1]))
                raw = rawmsg[0][1]
                raw_email_string = raw.decode('utf-8')
                msg = email.message_from_string(raw_email_string)
                #msg = email.message_from_bytes(str(rawmsg[0][1]).encode('ASCII'))
                body = ""
                if msg.is_multipart():
                    for part in msg.walk():
                        type = part.get_content_type()
                        disp = str(part.get('Content-Disposition'))
                        # look for plain text parts, but skip attachments
                        if type == 'text/plain' and 'attachment' not in disp:
                            charset = part.get_content_charset()
                            # decode the base64 unicode bytestring into plain text
                            body = part.get_payload(decode=True).decode(encoding=charset, errors="ignore")
                            # if we've found the plain/text part, stop looping thru the parts
                            messages.append({'num':num, 'body': body})
                            
                else:
                    # not multipart - i.e. plain text, no attachments
                    charset = msg.get_content_charset('ASCII')
                    if charset != None:
                        body = msg.get_payload(decode=True).decode(encoding=charset, errors="ignore")
                        messages.append({'num': num, 'body': body})
                    else:
                        body = msg.get_payload(decode=True)
                        print("body", body)
                        messages.append({'num': num, 'body': body})
        return messages

    def delete_message(self, msg_id):
        if not msg_id:
            return
        if self.move_to_trash:
            # move to Trash folder
            self.imap.uid('STORE', msg_id, '+FLAGS', '(Trash)')
            self.imap.expunge()
        else:
            self.imap.store(msg_id, '+FLAGS', '\\Deleted')
            self.imap.expunge()

问题是,每当我调用get_messages()类函数时,它都会返回一个错误,称为raw,这是一个整数,在google上搜索并阅读文章后,发现它应该返回一个整数而不是字节,所以我尝试将其转换为整数,但它没有decode属性,关于它为什么返回整数而不是字节,或者如何正确地将其解码为整数,有什么想法吗


Tags: thetoselfgetifbodymsgfolder