【问题标题】:How can you get the recipients of an email gmail python API如何获取电子邮件 gmail python API 的收件人
【发布时间】:2021-04-19 13:37:58
【问题描述】:
def getBody():
    """Shows basic usage of the Gmail API.
    Lists the user's Gmail labels.
    """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)
    service = build('gmail', 'v1', credentials=creds)
    # Call the Gmail API
    while True:
        labelName = "READ-BY-SCRIPT"
        LABEL_ID = 'Label_8507504117657095973'
        results = service.users().messages().list(
            userId='me', q="-label:"+labelName, maxResults=1).execute()
        messages = results.get('messages', [])
        body = []
        if not messages:
            TEACHER_NAME = "a "
            LINK = "https://google.com"
            DATE = "c"
            body = "d"
            return TEACHER_NAME, LINK, DATE, body
            break
        else:
            for message in messages:
                msg = service.users().messages().get(
                    userId='me', id=message['id']).execute()
                body.append(msg['payload']['parts'])
                if 'data' in body[0][0]['body']:
                    body = base64.urlsafe_b64decode(
                        body[0][0]['body']['data'])
                elif 'data' in body[0][1]['body']:
                    body = base64.urlsafe_b64decode(
                        body[0][1]['body']['data'])
                body = str(body)
                
                body = body.replace("\\\r", "\r").replace("\\\n", "\n").replace('\\xe2\\x80\\x99', "'").replace('\\xc3\\xa9', 'e').replace('\\xe2\\x80\\x90', '-').replace('\\xe2\\x80\\x91', '-').replace('\\xe2\\x80\\x92', '-').replace('\\xe2\\x80\\x93', '-').replace('\\xe2\\x80\\x94', '-').replace('\\xe2\\x80\\x94', '-').replace('\\xe2\\x80\\x98', "'").replace('\\xe2\\x80\\x9b', "'").replace('\\xe2\\x80\\x9c', '"').replace('\\xe2\\x80\\x9c', '"').replace(
                    '\\xe2\\x80\\x9d', '"').replace('\\xe2\\x80\\x9e', '"').replace('\\xe2\\x80\\x9f', '"').replace('\\xe2\\x80\\xa6', '...').replace('\\xe2\\x80\\xb2', "'").replace('\\xe2\\x80\\xb3', "'").replace('\\xe2\\x80\\xb4', "'").replace('\\xe2\\x80\\xb5', "'").replace('\\xe2\\x80\\xb6', "'").replace('\\xe2\\x80\\xb7', "'").replace('\\xe2\\x81\\xba', "+").replace('\\xe2\\x81\\xbb', "-").replace('\\xe2\\x81\\xbc', "=").replace('\\xe2\\x81\\xbd', "(").replace('\\xe2\\x81\\xbe', ")")
                return body

print(getBody())

此代码获取最新电子邮件的正文,并在稍微调整格式后打印正文。我如何才能获取电子邮件的收件人并将其打印出来?我似乎在文档中找不到任何关于它的信息:https://developers.google.com/gmail/api/reference/rest/v1/users.messages

【问题讨论】:

    标签: python python-3.x gmail gmail-api


    【解决方案1】:

    邮件的收件人可在原始邮件的headers 中找到(收件人、发件人、主题)。 headers[] 对象有如下描述:

    此消息部分的标头列表。对于表示整个消息有效负载的顶级消息部分,它将包含标准 RFC 2822 电子邮件标头,例如 To、From 和 Subject。

    headers = msg['payload']['headers']
    for header in headers:
        if header['name'] == 'To':
            print(header['value'])
    

    【讨论】:

      猜你喜欢
      • 2021-04-27
      • 1970-01-01
      • 2021-01-27
      • 2014-09-07
      • 2020-02-13
      • 1970-01-01
      • 2019-08-04
      • 2015-04-29
      • 1970-01-01
      相关资源
      最近更新 更多