Skip to main content
Glama
railsware

Mailtrap Email Sending

by railsware

send-email

Destructive

Send transactional emails via API to single or multiple recipients, including CC/BCC options and text/HTML content.

Instructions

Send an email to your recipient email address using Mailtrap Email API. You can send emails to multiple recipients at once.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
fromNoEmail address of the sender
toYesEmail address(es) of the recipient(s) - can be a single email or array of emails
subjectYesEmail subject line
ccNoOptional CC recipients
bccNoOptional BCC recipients
categoryYesEmail category for tracking
textNoEmail body text
htmlNoOptional HTML version of the email body

Implementation Reference

  • The handler function 'sendEmail' that executes the logic to send an email using the Mailtrap API client. Handles validation, normalization of addresses, and sends the email.
    import { Address, Mail } from "mailtrap";
    import { SendMailToolRequest } from "../../types/mailtrap";
    
    import { client } from "../../client";
    
    const { DEFAULT_FROM_EMAIL } = process.env;
    
    async function sendEmail({
      from,
      to,
      subject,
      text,
      cc,
      bcc,
      category,
      html,
    }: SendMailToolRequest): Promise<{ content: any[]; isError?: boolean }> {
      try {
        if (!client) {
          throw new Error("MAILTRAP_API_TOKEN environment variable is required");
        }
    
        if (!html && !text) {
          throw new Error("Either HTML or TEXT body is required");
        }
    
        const fromEmail = from ?? DEFAULT_FROM_EMAIL;
    
        if (!fromEmail) {
          throw new Error(
            "No 'from' email provided and no 'DEFAULT_FROM_EMAIL' email set"
          );
        }
    
        const fromAddress: Address = {
          email: fromEmail,
        };
    
        // Handle both single email and array of emails
        // Normalize inputs: convert to array, trim each email, filter empty strings
        const normalizedToEmails = (Array.isArray(to) ? to : [to])
          .map((email) => email.trim())
          .filter((email) => email.length > 0);
    
        // Validate that we have at least one valid recipient after normalization
        if (normalizedToEmails.length === 0) {
          throw new Error(
            "No valid recipients provided in 'to' field after normalization"
          );
        }
    
        const toAddresses: Address[] = normalizedToEmails.map((email) => ({
          email,
        }));
    
        const emailData: Mail = {
          from: fromAddress,
          to: toAddresses,
          subject,
          text,
          html,
          category,
        };
    
        if (cc && cc.length > 0) {
          const normalizedCcEmails = cc
            .map((email) => email.trim())
            .filter((email) => email.length > 0);
          if (normalizedCcEmails.length > 0) {
            emailData.cc = normalizedCcEmails.map((email) => ({ email }));
          }
        }
        if (bcc && bcc.length > 0) {
          const normalizedBccEmails = bcc
            .map((email) => email.trim())
            .filter((email) => email.length > 0);
          if (normalizedBccEmails.length > 0) {
            emailData.bcc = normalizedBccEmails.map((email) => ({ email }));
          }
        }
    
        const response = await client.send(emailData);
    
        return {
          content: [
            {
              type: "text",
              text: `Email sent successfully to ${toAddresses
                .map((addr) => addr.email)
                .join(", ")}.\nMessage IDs: ${response.message_ids}\nStatus: ${
                response.success ? "Success" : "Failed"
              }`,
            },
          ],
        };
      } catch (error) {
        const errorMessage = error instanceof Error ? error.message : String(error);
    
        return {
          content: [
            {
              type: "text",
              text: `Failed to send email: ${errorMessage}`,
            },
          ],
          isError: true,
        };
      }
    }
    
    export default sendEmail;
  • Defines the JSON schema for validating input parameters to the send-email tool, including from, to, subject, etc., with conditional requirements based on environment.
    const hasDefaultFromEmail = !!process.env.DEFAULT_FROM_EMAIL;
    
    const sendEmailSchema = {
      type: "object",
      properties: {
        from: {
          type: "string",
          format: "email",
          description: hasDefaultFromEmail
            ? "Email address of the sender (optional with default)"
            : "Email address of the sender",
        },
        to: {
          oneOf: [
            {
              type: "string",
              format: "email",
              description: "Single email address",
            },
            {
              type: "array",
              items: {
                type: "string",
                format: "email",
              },
              description: "Array of email addresses",
            },
          ],
          description:
            "Email address(es) of the recipient(s) - can be a single email or array of emails",
        },
        subject: {
          type: "string",
          description: "Email subject line",
        },
        cc: {
          type: "array",
          items: {
            type: "string",
            format: "email",
          },
          description: "Optional CC recipients",
        },
        bcc: {
          type: "array",
          items: {
            type: "string",
            format: "email",
          },
          description: "Optional BCC recipients",
        },
        category: {
          type: "string",
          description: "Email category for tracking",
        },
        text: {
          type: "string",
          description: "Email body text",
        },
        html: {
          type: "string",
          description: "Optional HTML version of the email body",
        },
      },
      required: ["to", "subject", "category"],
      additionalProperties: false,
    };
    
    if (hasDefaultFromEmail) {
      // Make from optional when default is available
      sendEmailSchema.required = sendEmailSchema.required.filter(
        (field: string) => field !== "from"
      );
    }
    
    export default sendEmailSchema;
  • src/server.ts:36-45 (registration)
    Registers the 'send-email' tool in the tools array used by the MCP server, linking the schema and handler.
    {
      name: "send-email",
      description:
        "Send an email to your recipient email address using Mailtrap Email API. You can send emails to multiple recipients at once.",
      inputSchema: sendEmailSchema,
      handler: sendEmail,
      annotations: {
        destructiveHint: true,
      },
    },
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

The annotations include 'destructiveHint: true', indicating this is a write operation with potential side effects. The description adds that it uses 'Mailtrap Email API' and supports multiple recipients, providing some context beyond annotations. However, it doesn't disclose additional behavioral traits like rate limits, authentication needs, or what 'destructive' entails (e.g., email delivery, no undo). With annotations covering the safety profile, a 3 is appropriate for adding moderate value.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is concise with two sentences: one stating the purpose and one adding a usage detail. It's front-loaded with the core functionality. However, the second sentence could be more informative (e.g., clarifying 'multiple recipients' vs. schema details), slightly reducing efficiency.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (8 parameters, destructive operation) and lack of output schema, the description is moderately complete. It covers the basic purpose and API context but lacks details on when to use, behavioral nuances, or return values. With annotations providing some safety info, it's adequate but has clear gaps for a mutation tool.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 100% description coverage, with clear documentation for all 8 parameters (e.g., 'from' as 'Email address of the sender'). The description doesn't add meaning beyond the schema, such as explaining parameter interactions or constraints. According to the rules, with high schema coverage (>80%), the baseline is 3 even without param info in the description.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Send an email to your recipient email address using Mailtrap Email API.' It specifies the action (send), resource (email), and technology (Mailtrap Email API). However, it doesn't explicitly differentiate from sibling tools like 'send-sandbox-email' or 'get-sandbox-messages', which would require a 5.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides minimal usage guidance: 'You can send emails to multiple recipients at once.' It doesn't specify when to use this tool versus alternatives like 'send-sandbox-email' (e.g., for testing vs. production), nor does it mention prerequisites, exclusions, or context for use. This lack of explicit guidance limits its effectiveness.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/railsware/mailtrap-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server