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

ParameterTypeRequiredDescription
rawstringYesThe 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?...?=.
threadIdstringNoThread 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

See all 12 tools

Toolkits in the same category

See all 100 toolkits

See Connect applied to your product.

Try it for 15 days with no card, or walk our team through your use case.