【问题标题】:Number of unread messages of gmailgmail未读消息数
【发布时间】:2016-02-22 14:11:26
【问题描述】:

如何使用带有python的无头树莓派获取gmail的未读消息数?

我用过

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/gmail-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Gmail API Python Quickstart'


def get_credentials()....

【问题讨论】:

  • 我没有看到任何尝试在您发布的代码中使用 API。

标签: python gmail raspberry-pi


【解决方案1】:
import imaplib
import email

def fetchUnreadEmails(profile, since=None, markRead=False, limit=None):

    conn = imaplib.IMAP4_SSL('imap.gmail.com')
    conn.debug = 0
    conn.login(profile['gmail_address'], 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 getDate(msg) > since:
                msgs.append(msg)
    conn.close()
    conn.logout()

    return msgs

来自jasperproject Gmail 模块

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-08
    • 2015-03-13
    • 2018-08-28
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多