sendEmail
Send emails directly from Gmail by specifying recipient, subject, and body content for communication and note management.
Instructions
Send an email from Gmail.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| to | Yes | Recipient email address. | |
| subject | Yes | Email subject. | |
| body | Yes | Email body. |
Implementation Reference
- src/index.ts:204-251 (handler)The main handler function for sendEmail that validates input, constructs a raw email message, encodes it in base64 URL-safe format, and sends it via Gmail APIcase "sendEmail": { if (!GMAIL_API_KEY) { return { content: [{ type: "text", text: "API Key not set." }] }; } try { const { to, subject, body } = SendEmailSchema.parse( request.params.arguments ); let emailHeaders = `To: ${to}\r\nSubject: ${subject}\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n`; const raw = Buffer.from(emailHeaders + body) .toString("base64") .replace(/\+/g, "-") .replace(/\//g, "_") .replace(/=+$/, ""); console.log("Raw message:", raw); const response = await fetch( `https://gmail.googleapis.com/gmail/v1/users/${GMAIL_USER_ID}/messages/send`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${GMAIL_API_KEY}`, }, body: JSON.stringify({ raw: raw }), } ); if (!response.ok) { const errorData = await response.json(); console.error("Gmail API Error:", errorData); // Debugging: Log the full error const errorMessage = errorData.error?.message || response.statusText; return { content: [{ type: "text", text: `Error: ${errorMessage}` }], }; } return { content: [{ type: "text", text: "Email sent successfully." }], }; } catch (error: any) { return { content: [{ type: "text", text: `Error: ${error.message}` }] }; } }
- src/index.ts:22-26 (schema)Zod schema definition for sendEmail tool input validation, defining required fields: to (email), subject (string), and body (string)const SendEmailSchema = z.object({ to: z.string().email().describe("Recipient email address."), subject: z.string().describe("Email subject."), body: z.string().describe("Email body."), });
- src/index.ts:70-74 (registration)Tool registration in the MCP server's ListToolsRequestSchema handler, exposing sendEmail with its description and input schema{ name: "sendEmail", description: "Send an email from Gmail.", inputSchema: zodToJsonSchema(SendEmailSchema), },