POST /functions/v1/send-gmail
Sends an email through Gmail using OAuth2 credentials. Supports plain text emails with subject and body.Request
Headers
Bearer token for authentication:
Bearer <your-supabase-jwt-token>Must be
application/jsonBody Parameters
Recipient email address. Can be a single email or name with email (e.g.,
"John Doe <[email protected]>").Email subject line.
Plain text content of the email body.
The workflow element ID. Used to load Gmail OAuth credentials from the database if not provided directly.
Google OAuth2 Client ID. If not provided, will be loaded from database based on elementId.
Google OAuth2 Client Secret. If not provided, will be loaded from database based on elementId.
Google OAuth2 Refresh Token. If not provided, will be loaded from database based on elementId.
Response
Indicates whether the email was sent successfully
Gmail message ID of the sent email
Email Format
Emails are sent in RFC 2822 format with the following structure:- Encoded in UTF-8
- Converted to base64
- URL-safe encoded (replacing
+with-,/with_, removing=) - Sent to Gmail API
Gmail OAuth Flow
- Load Credentials: Retrieves encrypted credentials from
user_gmail_credentialstable - Decrypt: Uses XOR decryption with
ENCRYPTION_KEYenvironment variable - Get Access Token: Exchanges refresh token for access token via Google OAuth
- Send Message: Posts to Gmail API with access token
Gmail API Endpoint Used
Examples
Request with Element ID
Request with Direct Credentials
Success Response
Error Response
Error Codes
| Status Code | Error Message | Description |
|---|---|---|
| 400 | Recipient email is required | Missing to parameter |
| 400 | Email subject is required | Missing subject parameter |
| 400 | Email body is required | Missing body parameter |
| 400 | Invalid Gmail credentials | Credentials are incomplete or incorrect |
| 404 | No Gmail credentials found for this user | User hasn’t connected Gmail account |
| 500 | Failed to retrieve Gmail credentials | Database error loading credentials |
| 500 | Failed to send email | Gmail API error or network issue |
Workflow Integration
When called fromrun-workflow, the function supports placeholder substitution in fields:
OAuth Token Refresh
The function automatically obtains a fresh access token:Configuration Priority
Credentials are loaded in this order:- Direct parameters:
clientId,clientSecret,refreshTokenfrom request - Database lookup: If elementId provided, loads from
user_gmail_credentialstable - Agent config check: Validates
agent_configshas entry (optional)
Notes
- Only plain text emails are supported (no HTML)
- The sender email is determined by the OAuth credentials (authenticated user’s Gmail)
- Character encoding is UTF-8 by default
- Emails are sent from the authenticated user’s Gmail account
- No attachments are supported in the current implementation
- Credentials are centrally managed in
user_gmail_credentialstable