create-template
Create reusable email templates with HTML and plain text versions for transactional emails in Mailtrap Email Sending.
Instructions
Create a new email template
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | Name of the template | |
| subject | Yes | Email subject line | |
| html | No | HTML content of the template (optional) | |
| text | No | Plain text version of the template (optional) | |
| category | No | Template category (optional, defaults to 'General') |
Implementation Reference
- The handler function that implements the logic for creating a new email template using the Mailtrap API client. It validates inputs, calls the API, and returns success or error content.async function createTemplate({ name, subject, html, text, category, }: CreateTemplateRequest): Promise<{ content: any[]; isError?: boolean }> { try { if (!client) { throw new Error("MAILTRAP_API_TOKEN environment variable is required"); } // Validate that at least one of html or text is provided if (!html && !text) { return { content: [ { type: "text", text: "Failed to create template: At least one of 'html' or 'text' content must be provided.", }, ], isError: true, }; } const createParams: any = { name, subject, category: category || "General", }; if (html) { createParams.body_html = html; } if (text) { createParams.body_text = text; } const template = await client.templates.create(createParams); return { content: [ { type: "text", text: `Template "${name}" created successfully!\nTemplate ID: ${template.id}\nTemplate UUID: ${template.uuid}`, }, ], }; } catch (error) { console.error("Error creating template:", error); const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: "text", text: `Failed to create template: ${errorMessage}`, }, ], isError: true, }; } }
- JSON schema defining the input parameters for the create-template tool, including name, subject, optional html/text content, and category.const createTemplateSchema = { type: "object", properties: { name: { type: "string", description: "Name of the template", }, subject: { type: "string", description: "Email subject line", }, html: { type: "string", description: "HTML content of the template (optional)", }, text: { type: "string", description: "Plain text version of the template (optional)", }, category: { type: "string", description: "Template category (optional, defaults to 'General')", }, }, required: ["name", "subject"], additionalProperties: false, };
- src/server.ts:46-54 (registration)Tool registration in the server's tools array, linking the name, description, schema, handler, and annotations.{ name: "create-template", description: "Create a new email template", inputSchema: createTemplateSchema, handler: createTemplate, annotations: { destructiveHint: true, }, },