The Gmail Handler enables seamless interaction with the Gmail API for sending, reading, and managing emails. It handles authentication using OAuth 2.0, and provides methods to fetch user profiles, read emails with attachments, send new emails, and create drafts. It is designed to integrate Gmail into automation workflows securely and efficiently.
To create the GmailHandler, initialize it with credentials JSON for OAuth:
Copy
from superagentx_handlers.gcp.gmail import GmailHandlergmail_handler = GmailHandler( credentials="credentials.json" # path to your OAuth 2.0 credentials file)
Get User Profile:
Retrieve Gmail profile information for the authenticated user.
Read Emails:
Fetches the latest emails, including sender, receiver, subject, body, and attachments.
Copy
emails = await gmail_handler.read_mail(max_results=10)for mail in emails: print(mail["subject"], mail["sender"])
Send Email:
Send an email via Gmail.
Copy
await gmail_handler.send_email( from_address="you@example.com", to="friend@example.com", subject="Hello from GmailHandler", content="This is a test email!")
Create Draft Email:
Create a draft email that can be sent later.
Copy
draft = await gmail_handler.create_draft_email( from_address="you@example.com", to="friend@example.com", subject="Draft subject", content="This is a saved draft message.")print(draft)