from apiclient import errors def ListMessagesMatchingQuery(service, user_id, query=''): """List all Messages of the user's mailbox matching the query. Args: service: Authorized Gmail API service instance. user_id: User's email address. The special value "me" can be used to indicate the authenticated user. query: String used to filter messages returned. Eg.- 'from:user@some_domain.com' for Messages from a particular sender. Returns: List of Messages that match the criteria of the query. Note that the returned list contains Message IDs, you must use get with the appropriate ID to get the details of a Message. """ try: response = service.users().messages().list(userId=user_id, q=query).execute() messages = [] if 'messages' in response: messages.extend(response['messages']) while 'nextPageToken' in response: page_token = response['nextPageToken'] response = service.users().messages().list(userId=user_id, q=query, pageToken=page_token).execute() messages.extend(response['messages']) return messages except errors.HttpError: print('An error occurred: %s' % error) def ListMessagesWithLabels(service, user_id, label_ids=[]): """List all Messages of the user's mailbox with label_ids applied. Args: service: Authorized Gmail API service instance. user_id: User's email address. The special value "me" can be used to indicate the authenticated user. label_ids: Only return Messages with these labelIds applied. Returns: List of Messages that have all required Labels applied. Note that the returned list contains Message IDs, you must use get with the appropriate id to get the details of a Message. """ try: response = service.users().messages().list(userId=user_id, labelIds=label_ids).execute() messages = [] if 'messages' in response: messages.extend(response['messages']) while 'nextPageToken' in response: page_token = response['nextPageToken'] response = service.users().messages().list(userId=user_id, labelIds=label_ids, pageToken=page_token).execute() messages.extend(response['messages']) return messages except errors.HttpError: print('An error occurred: %s' % error) """Get Message with given ID. """ import base64 import email from apiclient import errors def GetMessage(service, user_id, msg_id): """Get a Message with given ID. Args: service: Authorized Gmail API service instance. user_id: User's email address. The special value "me" can be used to indicate the authenticated user. msg_id: The ID of the Message required. Returns: A Message. """ try: message = service.users().messages().get(userId=user_id, id=msg_id).execute() return message except errors.HttpError: print('An error occurred: %s' % error) def get_message_header(message): return message['payload']['headers'] def GetMimeMessage(service, user_id, msg_id): """Get a Message and use it to create a MIME Message. Args: service: Authorized Gmail API service instance. user_id: User's email address. The special value "me" can be used to indicate the authenticated user. msg_id: The ID of the Message required. Returns: A MIME Message, consisting of data from Message. """ try: message = service.users().messages().get(userId=user_id, id=msg_id, format='raw').execute() #print 'Message snippet: %s' % message['snippet'] msg_str = base64.urlsafe_b64decode(message['raw'].encode('ASCII')) mime_msg = email.message_from_string(msg_str.decode()) return mime_msg except errors.HttpError: print('An error occurred: %s' % error) def get_message_body(mime_msg): body = "" if mime_msg.is_multipart(): for part in mime_msg.walk(): ctype = part.get_content_type() cdispo = str(part.get('Content-Disposition')) # skip any text/plain (txt) attachments if ctype == 'text/plain' and 'attachment' not in cdispo: body = part.get_payload() break # not multipart - i.e. plain text, no attachments, keeping fingers crossed else: body = mime_msg.get_payload() #print 'Message body: %s' % body return body