gmail.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:j2f 作者: jasper2fork 项目源码 文件源码
def fetch_unread_emails(self, since=None, markRead=False, limit=None):
        """
            Fetches a list of unread email objects from a user's Gmail inbox.

            Arguments:
            since -- if provided, no emails before this date will be returned
            markRead -- if True, marks all returned emails as read in target
                        inbox

            Returns:
            A list of unread email objects.
        """
        conn = imaplib.IMAP4_SSL('imap.gmail.com')
        conn.debug = 0
        conn.login(self.profile['gmail_address'],
                   self.profile['gmail_password'])
        conn.select(readonly=(not markRead))

        msgs = []
        (retcode, messages) = conn.search(None, '(UNSEEN)')

        if retcode == 'OK' and messages != ['']:
            numUnread = len(messages[0].split(' '))
            if limit and numUnread > limit:
                return numUnread

            for num in messages[0].split(' '):
                # parse email RFC822 format
                ret, data = conn.fetch(num, '(RFC822)')
                msg = email.message_from_string(data[0][1])

                if not since or get_date(msg) > since:
                    msgs.append(msg)
        conn.close()
        conn.logout()

        return msgs
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号