Send message with the Gmail API
Sends an email right away to the recipients in the To, Cc and Bcc headers of raw, and returns the sent message with id, threadId and labelIds. To reply inside a conversation, pass its threadId, keep the same Subject and set the In-Reply-To and References headers to the Message-ID of the message answered, which Get message returns in payload.headers.
- Identifier
- gmail_send_message
The request
POST https://connect.reportei.com/api/tools/gmail_send_message/execute
Authorization: Bearer <merchant token>
x-customer-token: <customer token>
Content-Type: application/json
Accept: application/json
{
"customer_toolkit": "<connection UUID>",
"arguments": {
"raw": "RnJvbTogc2VuZGVyQGV4YW1wbGUuY29tDQpUbzogcmVjaXBpZW50QGV4YW1wbGUuY29tDQpTdWJqZWN0OiBDb25uZWN0IGV4YW1wbGUNCk1JTUUtVmVyc2lvbjogMS4wDQpDb250ZW50LVR5cGU6IG11bHRpcGFydC9hbHRlcm5hdGl2ZTsgYm91bmRhcnk9ImNvbm5lY3QtZXhhbXBsZSINCg0KLS1jb25uZWN0LWV4YW1wbGUNCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbjsgY2hhcnNldD0iVVRGLTgiDQpDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiA4Yml0DQoNCkhlbGxvISBUaGlzIGlzIGFuIGV4YW1wbGUgbWVzc2FnZS4NCg0KLS1jb25uZWN0LWV4YW1wbGUNCkNvbnRlbnQtVHlwZTogdGV4dC9odG1sOyBjaGFyc2V0PSJVVEYtOCINCkNvbnRlbnQtVHJhbnNmZXItRW5jb2Rpbmc6IDhiaXQNCg0KPHAgc3R5bGU9Im1hcmdpbjowIDAgMTJweCAwO2ZvbnQtZmFtaWx5OkFyaWFsLHNhbnMtc2VyaWY7Zm9udC1zaXplOjE0cHg7bGluZS1oZWlnaHQ6MS40OyI-SGVsbG8hIFRoaXMgaXMgYW4gZXhhbXBsZSBtZXNzYWdlLjwvcD4NCi0tY29ubmVjdC1leGFtcGxlLS0NCg"
}
}Your system sends your customer's connection id and the tool arguments. The headers are the same as the rest of the Connect API: your product's token and your customer's token.
Illustrative example. Replace the tokens, connection UUID and destination values with your own. This page does not execute the request.
Before executing
- raw holds the complete MIME message encoded as base64url, not just the email body. This example includes UTF-8 text/plain and text/html parts, with CRLF between MIME lines.
- Before encoding, replace sender@example.com with the connected account or an authorized alias, and recipient@example.com with the recipient. Use a MIME library to encode non-ASCII subject headers.
- For a reply in a thread, supply threadId, keep the Subject and include References and In-Reply-To for the corresponding Message-ID. The connection needs a Gmail scope that permits sending.
Prepare raw in JavaScript (Node.js)
// Node.js: replace the example addresses before encoding.
const mime = [
"From: sender@example.com",
"To: recipient@example.com",
"Subject: Connect example",
"MIME-Version: 1.0",
"Content-Type: multipart/alternative; boundary=\"connect-example\"",
"",
"--connect-example",
"Content-Type: text/plain; charset=\"UTF-8\"",
"Content-Transfer-Encoding: 8bit",
"",
"Hello! This is an example message.",
"",
"--connect-example",
"Content-Type: text/html; charset=\"UTF-8\"",
"Content-Transfer-Encoding: 8bit",
"",
"<p style=\"margin:0 0 12px 0;font-family:Arial,sans-serif;font-size:14px;line-height:1.4;\">Hello! This is an example message.</p>",
"--connect-example--",
""
].join("\r\n");
const raw = Buffer.from(mime, "utf8").toString("base64url");
const body = {
customer_toolkit: "<connection UUID>",
arguments: { raw }
};
// Serialize body with JSON.stringify(body) in your HTTP client.Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| raw | string | Yes | The whole email in RFC 2822 format, base64url encoded: the header lines, such as To, Cc, Subject and Content-Type: text/plain; charset=UTF-8, a blank line and the body. A subject with accents goes encoded as =?UTF-8?B?...?=. |
| threadId | string | No | Thread id from List messages, Get message or Get thread, to send the email as a reply in that conversation. |
Supported connections
Your customer connects their Gmail account once, with one of the modes below. The same connection serves every tool of the toolkit.
OAuth with your product's app
You register your product's OAuth app once, with POST /api/merchants/credentials/gmail, and your customer authorizes the account on a consent screen. Connect renews the token when it is about to expire.
The response
On success, Connect preserves the app's response status and body. HTTP errors returned by the app use the {error, api_error} envelope; authentication or validation can fail before execution. The X-Toolkit-Execution-Id header identifies the recorded execution, available at GET /api/tools/executions/{uuid}.
Illustrative success format
{
"id": "example-message-id",
"threadId": "example-thread-id",
"labelIds": [
"SENT"
]
}The content below is an example, not a recorded execution. Save X-Toolkit-Execution-Id when returned to look up the execution. Connect provides no retries or idempotency: repeating a POST may send the message again.
Ready for AI agents
This tool's input is a JSON Schema, available at GET /api/tools/gmail_send_message. It becomes your agent's tool definition with no conversion.
Other Gmail tools
- List messagesGETLists the messages of the mailbox that match q and labelIds. Each item has only id and threadId: read the content with Get message. Pass nextPageToken as pageToken to read the next page.
- Get messageGETGets a message with its threadId, labelIds and snippet. With format full, the default, payload has the headers (From, To, Subject, Date, Message-ID) and the body parts, whose data is base64url encoded. Attachments come only as an attachmentId and are not downloaded.
- Get threadGETGets a conversation with all its messages. Each message has id, labelIds, snippet and, with format full, the default, the payload with the headers and the base64url encoded body parts.
- Update message labelsPOSTAdds and removes labels on a message and returns its labelIds. Remove UNREAD to mark it as read, remove INBOX to archive it, add STARRED to star it, or add TRASH to move it to the trash.
- List labelsGETLists the labels of the mailbox: the system labels, such as INBOX, SENT, DRAFT, UNREAD, STARRED and SPAM, and the labels the user created. The id of each one is what labelIds, addLabelIds and removeLabelIds take.
- Create labelPOSTCreates a label and returns it with its id, to apply with Update message labels. The name cannot be the name of a system label or of an existing label.
- List draftsGETLists the drafts of the mailbox. Each item has the draft id and a message with id and threadId: read the content with Get message using message.id. Pass nextPageToken as pageToken to read the next page.
- Create draftPOSTSaves an email as a draft without sending it, and returns the draft id and its message with id and threadId. Send it later with Send draft.
See Connect applied to your product.
Try it for 15 days with no card, or walk our team through your use case.
