send_email
Send emails programmatically using the Gmail API. Specify recipients, subject, and content, including HTML. Manage CC and BCC fields for efficient email distribution.
Instructions
Send a new email
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| bcc | No | BCC recipients (comma-separated) | |
| body | Yes | Email body (can include HTML) | |
| cc | No | CC recipients (comma-separated) | |
| subject | Yes | Email subject | |
| to | Yes | Recipient email address |
Implementation Reference
- src/index.ts:450-502 (handler)The handler function that parses arguments, constructs a raw base64url-encoded email message with headers and body, and sends it using the Gmail API.private async handleSendEmail(args: any) { try { const { to, subject, body, cc, bcc } = args; const headers = [ 'Content-Type: text/plain; charset="UTF-8"', "MIME-Version: 1.0", `To: ${to}`, cc ? `Cc: ${cc}` : null, bcc ? `Bcc: ${bcc}` : null, `Subject: ${subject}`, ] .filter(Boolean) .join("\r\n"); // Ensure proper separation between headers and body const email = `${headers}\r\n\r\n${body}`; // Encode in base64url const encodedMessage = Buffer.from(email) .toString("base64") .replace(/\+/g, "-") .replace(/\//g, "_") .replace(/=+$/, ""); // Send the email const response = await this.gmail.users.messages.send({ userId: "me", requestBody: { raw: encodedMessage, }, }); return { content: [ { type: "text", text: `Email sent successfully. Message ID: ${response.data.id}`, }, ], }; } catch (error: any) { return { content: [ { type: "text", text: `Error sending email: ${error.message}`, }, ], isError: true, }; } }
- src/index.ts:106-135 (schema)The input schema and metadata for the send_email tool, specifying required parameters to, subject, body and optional cc, bcc.{ name: "send_email", description: "Send a new email", inputSchema: { type: "object", properties: { to: { type: "string", description: "Recipient email address", }, subject: { type: "string", description: "Email subject", }, body: { type: "string", description: "Email body (can include HTML)", }, cc: { type: "string", description: "CC recipients (comma-separated)", }, bcc: { type: "string", description: "BCC recipients (comma-separated)", }, }, required: ["to", "subject", "body"], }, },
- src/index.ts:278-279 (registration)Registration in the CallToolRequestHandler switch statement, dispatching send_email calls to the handleSendEmail method.case "send_email": return await this.handleSendEmail(request.params.arguments);