send-email
Send emails to recipients with HTML support, CC/BCC options, and template management through the SMTP MCP Server.
Instructions
Send an email to one or more recipients
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| to | Yes | Array of recipients | |
| subject | Yes | Email subject | |
| body | Yes | Email body (HTML supported) | |
| from | No | Sender information. If not provided, the default SMTP user will be used. | |
| cc | No | Array of CC recipients | |
| bcc | No | Array of BCC recipients | |
| templateId | No | ID of the email template to use. If not provided, the email will use the subject and body provided. | |
| templateData | No | Data to be used for template variable substitution | |
| smtpConfigId | No | ID of the SMTP configuration to use. If not provided, the default configuration will be used. |
Implementation Reference
- src/tools.ts:20-93 (schema)Input schema and description for the 'send-email' tool, defining parameters like to, subject, body, cc, bcc, templates, etc."send-email": { name: "send-email", description: "Send an email to one or more recipients", inputSchema: { type: "object", properties: { to: { type: "array", items: { type: "object", properties: { email: { type: "string" }, name: { type: "string" } }, required: ["email"] }, description: "Array of recipients" }, subject: { type: "string", description: "Email subject" }, body: { type: "string", description: "Email body (HTML supported)" }, from: { type: "object", properties: { email: { type: "string" }, name: { type: "string" } }, description: "Sender information. If not provided, the default SMTP user will be used." }, cc: { type: "array", items: { type: "object", properties: { email: { type: "string" }, name: { type: "string" } }, required: ["email"] }, description: "Array of CC recipients" }, bcc: { type: "array", items: { type: "object", properties: { email: { type: "string" }, name: { type: "string" } }, required: ["email"] }, description: "Array of BCC recipients" }, templateId: { type: "string", description: "ID of the email template to use. If not provided, the email will use the subject and body provided." }, templateData: { type: "object", description: "Data to be used for template variable substitution" }, smtpConfigId: { type: "string", description: "ID of the SMTP configuration to use. If not provided, the default configuration will be used." } }, required: ["to", "subject", "body"] } },
- src/index.ts:59-62 (registration)Registers all tools including 'send-email' by calling createToolDefinitions() and passing to setupRequestHandlers(server, TOOLS). This makes the tool available via MCP.const TOOLS = createToolDefinitions(); // Setup request handlers await setupRequestHandlers(server, TOOLS);
- src/requestHandler.ts:69-71 (registration)Dispatches 'send-email' tool calls to the handleSendEmail function in the CallToolRequestSchema handler.case "send-email": return await handleSendEmail(toolParams);
- src/requestHandler.ts:142-174 (handler)Handler wrapper that normalizes input parameters and delegates to the core sendEmail function from emailService.async function handleSendEmail(parameters: any) { try { // If "to" is a single object, convert it to an array const to = Array.isArray(parameters.to) ? parameters.to : [parameters.to]; // Prepare the email data const emailData: EmailData = { to: to, subject: parameters.subject, body: parameters.body, from: parameters.from, cc: parameters.cc, bcc: parameters.bcc, templateId: parameters.templateId, templateData: parameters.templateData }; // Send the email const result = await sendEmail(emailData, parameters.smtpConfigId); return { success: result.success, message: result.message }; } catch (error) { logToFile('Error in handleSendEmail:'); logToFile(error instanceof Error ? error.message : 'Unknown error'); return { success: false, message: error instanceof Error ? error.message : 'Unknown error' }; } }
- src/emailService.ts:151-226 (handler)Core implementation of email sending using nodemailer: creates transport, processes templates, formats recipients, sends mail, and logs activity.export async function sendEmail(data: EmailData, smtpConfigId?: string): Promise<{ success: boolean; message?: string }> { try { const transport = await createTransport(smtpConfigId); const smtpConfig = smtpConfigId ? (await getSmtpConfigs()).find(c => c.id === smtpConfigId) : await getDefaultSmtpConfig(); if (!smtpConfig) { return { success: false, message: 'SMTP configuration not found' }; } // Generate email content from template if templateId is provided const { subject, body } = await generateEmailContent( data.templateId, data.templateData, data.subject, data.body ); // Create mail options const mailOptions = { from: data.from ? (data.from.name ? `"${data.from.name}" <${data.from.email}>` : data.from.email) : (smtpConfig.auth.user), to: formatRecipients(data.to), subject, html: body, cc: data.cc ? formatRecipients(data.cc) : undefined, bcc: data.bcc ? formatRecipients(data.bcc) : undefined }; // Send email const info = await transport.sendMail(mailOptions); // Log email activity const recipients = Array.isArray(data.to) ? data.to : [data.to]; for (const recipient of recipients) { const logEntry: EmailLogEntry = { timestamp: new Date().toISOString(), smtpConfig: smtpConfig.id, templateId: data.templateId, recipient: recipient.email, subject, success: true, message: `Message sent: ${info.messageId}` }; await logEmailActivity(logEntry); } return { success: true, message: `Message sent: ${info.messageId}` }; } catch (error) { logToFile(`Error sending email: ${error}`); // Log failed email activity if (data.to) { const recipients = Array.isArray(data.to) ? data.to : [data.to]; for (const recipient of recipients) { const logEntry: EmailLogEntry = { timestamp: new Date().toISOString(), smtpConfig: smtpConfigId || 'unknown', templateId: data.templateId, recipient: recipient.email, subject: data.subject, success: false, message: error instanceof Error ? error.message : 'Unknown error sending email' }; await logEmailActivity(logEntry); } } return { success: false, message: error instanceof Error ? error.message : 'Unknown error sending email' }; } }