send_email
Send emails through Gmail SMTP by providing recipient address, subject line, and message body content.
Instructions
Send an email using Gmail SMTP
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| to | Yes | Recipient email address | |
| subject | Yes | Email subject | |
| body | Yes | Email body content |
Implementation Reference
- src/index.ts:70-82 (handler)MCP tool handler for 'send_email' that invokes the internal sendEmail function and returns formatted content with structured result.async ({ to, subject, body }) => { const result = await sendEmail(to, subject, body); return { content: [{ type: 'text', text: result.success ? `Email sent to ${to}. Message ID: ${result.messageId}` : `Failed to send email: ${result.error}` }], structuredContent: result }; }
- src/index.ts:59-68 (schema)Input schema (to, subject, body with Zod validation) and output schema (success, messageId, error) for the send_email tool.inputSchema: { to: z.string().email().describe('Recipient email address'), subject: z.string().min(1).describe('Email subject'), body: z.string().min(1).describe('Email body content') }, outputSchema: { success: z.boolean().describe('Whether email was sent successfully'), messageId: z.string().optional().describe('Message ID from Gmail'), error: z.string().optional().describe('Error message if failed') }
- src/index.ts:54-83 (registration)Registration of the 'send_email' tool on the MCP server with schema and handler.server.registerTool( 'send_email', { title: 'Send Email', description: 'Send an email using Gmail SMTP', inputSchema: { to: z.string().email().describe('Recipient email address'), subject: z.string().min(1).describe('Email subject'), body: z.string().min(1).describe('Email body content') }, outputSchema: { success: z.boolean().describe('Whether email was sent successfully'), messageId: z.string().optional().describe('Message ID from Gmail'), error: z.string().optional().describe('Error message if failed') } }, async ({ to, subject, body }) => { const result = await sendEmail(to, subject, body); return { content: [{ type: 'text', text: result.success ? `Email sent to ${to}. Message ID: ${result.messageId}` : `Failed to send email: ${result.error}` }], structuredContent: result }; } );
- src/index.ts:25-46 (helper)Internal helper function that performs the actual email sending using nodemailer and Gmail transporter.async function sendEmail(to: string, subject: string, body: string) { const transporter = createEmailTransporter(); try { const info = await transporter.sendMail({ from: process.env.GMAIL_USER, to, subject, text: body, }); return { success: true, messageId: info.messageId, }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Unknown error', }; } }
- src/index.ts:11-23 (helper)Helper function to create and configure the nodemailer transporter for Gmail SMTP using environment variables.function createEmailTransporter() { const user = process.env.GMAIL_USER; const pass = process.env.GMAIL_PASS; if (!user || !pass) { throw new Error('Gmail credentials not found. Set GMAIL_USER and GMAIL_PASS environment variables.'); } return nodemailer.createTransport({ service: 'gmail', auth: { user, pass }, }); }