简体   繁体   English

您如何获得 email gmail python API 的收件人

[英]How can you get the recipients of an email gmail python API

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())

This code gets the body of the most recent email and after adjusting format a bit, prints the body.此代码获取最新的 email 的正文,并在稍微调整格式后打印正文。 How can I instead get the recipients of the email and print those out?我怎样才能获得 email 的收件人并将其打印出来? I couldn't seem to find anything about it on the documentation: https://developers.google.com/gmail/api/reference/rest/v1/users.messages我似乎无法在文档中找到任何相关信息: https://developers.google.com/gmail/api/reference/rest/v1/users.messages

The recipients of a message are available in the headers of the raw message (To, From, Subject).邮件的收件人在原始邮件的标题中可用(收件人、发件人、主题)。 The headers[] object has the following description: headers[] object 具有以下描述:

List of headers on this message part.此消息部分的标头列表。 For the top-level message part, representing the entire message payload, it will contain the standard RFC 2822 email headers such as To, From, and Subject.对于代表整个消息负载的顶级消息部分,它将包含标准的 RFC 2822 email 标头,例如 To、From 和 Subject。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取电子邮件 gmail python API 的发件人 - How can I get the sender of an email gmail python API 您可以使用由服务帐户在 python 中验证的 Gmail API 发送电子邮件吗? - Can you send email with Gmail API authenticated by Service Account in python? Using the gmail python API how can I get the most recent email that does not have a label “read” - Using the gmail python API how can I get the most recent email that does not have a label “read” Python:如何从 gmail API 获取电子邮件的主题 - Python: how to get the subject of an email from gmail API Python:无法将电子邮件发送给多个收件人 - Python: can't get email to send to multiple recipients 如何使用Gmail Python API获取电子邮件发件人的电子邮件地址 - How to get the email-address of the sender of an email using Gmail Python API 如何使用 python ZDB974238714CA8DE634A7CE1D0F 打印 GMail email 的主题和正文? - How can I print the subject and body of a GMail email using the python API? 通过电子邮件向多个收件人发送Python - Email multiple recipients Python 为什么不能得到 email 消息(python,gmail,django)? - Why can not get email message (python, gmail, django)? 如何使用Python将大文件附加到电子邮件 - Gmail API - How to attach large files to an email using Python - Gmail API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM