import base64 from googleapiclient.discovery import build from google.oauth2 import service_account def get_latest_email(): # Replace with your own Gmail service account setup creds = service_account.Credentials.from_service_account_file( 'credentials.json', scopes=['https://www.googleapis.com/auth/gmail.readonly'] ) service = build('gmail', 'v1', credentials=creds) results = service.users().messages().list(userId='me', labelIds=['INBOX'], maxResults=1).execute() msg = results['messages'][0] msg_data = service.users().messages().get(userId='me', id=msg['id']).execute() payload = msg_data['payload'] data = payload['body']['data'] return base64.urlsafe_b64decode(data).decode("utf-8")