简体   繁体   English

有没有办法使用Gmail API来获取两个用户发送给我的gmail的第一封电子邮件? 我的代码首先送给一个用户给我

[英]Is there a way to get the first emails sent by two users to my gmail by using Gmail API? My code gets for one users first email to me

This code gets me a particular users first message to me in gmail using gmail api. 此代码使我使用gmail api在gmail中向我发送了特定用户的第一条消息。

How to get two particular users first message in my gmail using gmail-api Can i use if else statements here? 如何使用gmail-api在我的gmail中获得两个特定用户的第一条消息,是否可以在此处使用if else语句?

from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import time

SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'

def main():

    store = file.Storage('token.json')
    creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=creds.authorize(Http()))

# Call the Gmail API to fetch INBOX
#results = service.users().messages().list(userId='me',maxResults=10,labelIds = ['INBOX']).execute()
results = service.users().messages().list(userId='me', q='googlecommunityteam-noreply@google.com',labelIds = ['INBOX']).execute()

messages = results.get('messages', [])



if not messages:
    print ("\nGmail: \n No gmail mails found.\n")
else:
    print ("Gmail:\n")

    msg = service.users().messages().get(userId='me', id=messages[0]['id']).execute()
    print(msg['snippet'])
    print(msg['internalDate'])




if __name__ == '__main__':
    main()

The Q paramater in Gmail searching works exactly like searching in the Gmail website so if you go to gmail and click on the arrow to the right in the search box. Gmail搜索中的Q参数与Gmail网站中的搜索完全一样,因此,如果您使用gmail并单击搜索框中右侧的箭头,则该参数将与Gmail中的参数完全相同。 Up will pop the search builder 向上将弹出搜索构建器

在此处输入图片说明

You can play around in this and bring up any type of search you want 您可以在其中玩转,并调出您想要的任何类型的搜索

from:(Test@gmail.com) to:(me@gmail.com)

You can then take this and dump it directly into the q in your request 然后,您可以将其直接放入请求中的q中

results = service.users().messages().list(userId='me', q='from:(Test@gmail.com) to:(me@gmail.com)',labelIds = ['INBOX']).execute()

Now all that being said i played around it and i was unable to get it to return emails by two different users if you put in two email addresses it only returns emails where they both where in the field. 现在所有这些我都在玩,如果您输入两个电子邮件地址,我将无法使它返回两个不同用户的电子邮件,它只能返回他们在现场的任何地方的电子邮件。 So the only way i think you are going to be able to find the first email sent to you by two diffrent users would be to do 因此,我认为您将能够找到两个不同用户发送给您的第一封电子邮件的唯一方法是

results = service.users().messages().list(userId='me', q='from:(user1@gmail.com) to:(me@gmail.com)',labelIds = ['INBOX']).execute()
results = service.users().messages().list(userId='me', q='from:(user2@gmail.com) to:(me@gmail.com)',labelIds = ['INBOX']).execute()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM