Пример #1
0
    def imapCycle(self):
        self.logger.info('Connecting to %s...', repr(self.imapHost))
        imap = IMAPClient(self.imapHost)

        self.logger.info('Logging in...')
        imap.login(self.imapUser, self.imapPass)

        self.logger.info('Selecting INBOX...')
        ret = imap.select_folder("INBOX")
        num = ret[b'EXISTS']
        self.logger.info(u'Selected mailbox with %s messages.', num)

        self.logger.info(u'Starting IMAP idle mode.')
        imap.idle()

        while self.run:
            try:
                # Wait for an IDLE response
                imap.idle_check(timeout = 60 * 5)
                # TODO: process response and stay in idle mode
            except:
                self.logger.error('IMAP error:', exc_info = True)
                break

            imap.idle_done()
            self.fetchNewMails(imap)
            imap.idle()

        imap.close()
        imap.logout()
Пример #2
0
class Imapmail(object):
    def __init__(self):  # 初始化数据
        self.serveraddress = None
        self.user = None
        self.passwd = None
        self.prot = None
        self.ssl = None
        self.timeout = None
        self.savepath = None
        self.server = None

    def client(self):  # 链接
        try:
            self.server = IMAPClient(self.serveraddress,
                                     self.prot,
                                     self.ssl,
                                     timeout=self.timeout)
            return self.server
        except BaseException as e:
            return "ERROR: >>> " + str(e)

    def login(self):  # 认证
        try:
            self.server.login(self.user, self.passwd)
        except BaseException as e:
            return "ERROR: >>> " + str(e)

    def getmaildir(
            self
    ):  # 获取目录列表 [((), b'/', 'INBOX'), ((b'\\Drafts',), b'/', '草稿箱'),]
        dirlist = self.server.list_folders()
        return dirlist

    def getallmail(self):  # 收取所有邮件
        gitaddr = []
        print(self.server)
        self.server.select_folder(
            'INBOX', readonly=True)  # 选择目录 readonly=True 只读,不修改,这里只选择了 收件箱
        result = self.server.search()  # 获取所有邮件总数目 [1,2,3,....]
        print("邮件列表:", result)
        for _sm in result:
            # data = self.server.fetch(_sm, ['ENVELOPE'])
            # size = self.server.fetch(_sm, ['RFC822.SIZE'])
            # print("大小", size)
            # envelope = data[_sm][b'ENVELOPE']
            # print(envelope)
            # subject = envelope.subject.decode()
            # if subject:
            #     subject, de = decode_header(subject)[0]
            #     subject = subject if not de else subject.decode(de)
            # dates = envelope.date
            # print("主题", subject)
            # print("时间", dates)

            msgdict = self.server.fetch(_sm, ['BODY[]'])  # 获取邮件内容
            mailbody = msgdict[_sm][b'BODY[]']  # 获取邮件内容
            soup = BeautifulSoup(mailbody)
            gitaddr.append(soup.find_all('td')[5].string)
        data = set(gitaddr)
        print(data)
        return data
        # for td in soup.find_all('td'):
        #     # gitaddr = re.match('git', str(td.string)).start()
        #     if td.string == None:
        #         pass
        #     else:
        #         print(td.string)
        # with open(self.savepath + str(_sm), 'wb') as f:  # 存放邮件内容
        #     f.write(mailbody)

    def close(self):
        self.server.close()