# How to Connect Gmail

## Overview

Automation Studio supports Gmail integration through OAuth2, allowing your flows to send emails from your Gmail account.

## Step 1: Set Up Gmail OAuth2

1. Go to [Google Cloud Console](https://console.cloud.google.com/)
2. Create a new project or select an existing one
3. Enable Gmail API:
   - Go to "APIs & Services" → "Library"
   - Search for "Gmail API"
   - Click "Enable"
4. Create OAuth2 credentials:
   - Go to "APIs & Services" → "Credentials"
   - Click "Create Credentials" → "OAuth client ID"
   - Choose "Web application"
   - Add authorized redirect URI: `https://your-domain.com/api/auth/gmail/callback`
   - Copy the Client ID and Client Secret

## Step 2: Get OAuth Token

1. Use Google's OAuth2 Playground or your own OAuth flow
2. Request scopes: `https://www.googleapis.com/auth/gmail.send`
3. Exchange authorization code for access token
4. Copy the access token and refresh token

## Step 3: Add Integration in Automation Studio

1. Open Automation Studio
2. Click the **🔗 Registry** button (top-right)
3. Click **+ Add Integration**
4. Select **Gmail** as the provider
5. Enter a name (e.g., "Support Email")
6. Enter your Gmail address
7. Paste your OAuth access token (will be stored securely)
8. Optionally add refresh token for automatic renewal
9. Click **Save**
10. Click **Test** to verify the connection

## Step 4: Use in Flows

1. In Flow Builder, add an **Email** node
2. In the node configuration:
   - Select your Gmail integration from the dropdown
   - Enter recipient email (To)
   - Enter subject and body
   - Optionally add CC/BCC
3. Save and test your flow

## Alternative: SMTP Configuration

If OAuth2 is not available, you can configure Gmail via SMTP:

1. Enable "Less secure app access" in your Google Account (not recommended)
2. Or use App Password:
   - Go to Google Account → Security
   - Enable 2-Step Verification
   - Generate App Password
   - Use this password in SMTP configuration

## Testing

Use the **Test** button in the Integrations page to send a test email. You should see:
- ✅ **Connected** status if successful
- ⚠️ **Error** status if there's an issue

## Troubleshooting

- **OAuth token expired**: Refresh the token or re-authenticate
- **Permission denied**: Check that Gmail API is enabled and scopes are correct
- **SMTP connection failed**: Verify App Password is correct

## Security

- OAuth tokens are stored securely in encrypted vault
- Never share OAuth credentials
- Rotate tokens periodically
- Use App Passwords instead of main password for SMTP

