> ## Documentation Index
> Fetch the complete documentation index at: https://docs.superagentx.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Gmail Handler

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.

## Example

To create the GmailHandler, initialize it with credentials JSON for OAuth:

```python theme={null}
from superagentx_handlers.gcp.gmail import GmailHandler

gmail_handler = GmailHandler(
    credentials="credentials.json"  # path to your OAuth 2.0 credentials file
)
```

**Get User Profile:** <br />
Retrieve Gmail profile information for the authenticated user.

```python theme={null}
profile = await gmail_handler.get_user_profile()
print(profile)
```

**Read Emails:** <br />
Fetches the latest emails, including sender, receiver, subject, body, and attachments.

```python theme={null}
emails = await gmail_handler.read_mail(max_results=10)
for mail in emails:
    print(mail["subject"], mail["sender"])
```

**Send Email:** <br />
Send an email via Gmail.

```python theme={null}
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:** <br />
Create a draft email that can be sent later.

```python theme={null}
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)
```
