mailpit-api - v1.5.3
    Preparing search index...

    Class MailpitClient

    Client for interacting with the Mailpit API.

    import { MailpitClient } from "mailpit-api";
    const mailpit = new MailpitClient("http://localhost:8025");
    console.log(await mailpit.getInfo());
    Index

    Constructors

    • Creates an instance of MailpitClient.

      Parameters

      • baseURL: string

        The base URL of the Mailpit API.

      • Optionalauth: { password: string; username: string }

        Optional authentication credentials.

        • password: string

          The password for basic authentication.

        • username: string

          The username for basic authentication.

      Returns MailpitClient

      const mailpit = new MailpitClient("http://localhost:8025");
      
      const mailpit = new MailpitClient("http://localhost:8025", {
      username: "admin",
      password: "supersecret",
      });

    Methods

    • Delete individual or all messages.

      Parameters

      Returns Promise<string>

      Plain text "ok" response

      If no IDs are provided then all messages are deleted.

      // Delete all messages
      await mailpit.deleteMessages();

      // Delete specific messages
      await mailpit.deleteMessages({ IDs: ["1", "2", "3"] });
    • Delete all messages matching a search.

      Parameters

      Returns Promise<string>

      Plain text "ok" response

      // Delete all messages from the domain example.test
      await mailpit.deleteMessagesBySearch({query: "from:example.test"});
    • Deletes a tag from all messages.

      Parameters

      • tag: string

        The name of the tag to delete.

      Returns Promise<string>

      Plain text "ok" response

      await mailpit.deleteTag("Tag 1");
      

      This does NOT delete any messages

    • Generates a cropped 180x120 JPEG thumbnail of an image attachment from a message. Only image attachments are supported.

      Parameters

      • id: string

        Message database ID or "latest"

      • partID: string

        The attachment part ID

      Returns Promise<MailpitAttachmentDataResponse>

      Image attachment thumbnail as binary data and the content type

      If the image is smaller than 180x120 then the image is padded. If the attachment is not an image then a blank image is returned.

      const message = await mailpit.getMessageSummary();
      if (message.Attachments.length) {
      const thumbnail = await mailpit.getAttachmentThumbnail(message.ID, message.Attachments[0].PartID);
      // Do something with the thumbnail data
      }
    • Retrieves the current Chaos triggers configuration (if enabled).

      Returns Promise<MailpitChaosTriggersResponse>

      The Chaos triggers configuration

      This will return an error if Chaos is not enabled at runtime.

      const triggers = await mailpit.getChaosTriggers();
      
    • Retrieves information about the Mailpit instance.

      Returns Promise<MailpitInfoResponse>

      Basic runtime information, message totals and latest release version.

      const info = await mailpit.getInfo();
      
    • Retrieves a specific attachment from a message.

      Parameters

      • id: string

        Message database ID or "latest"

      • partID: string

        The attachment part ID

      Returns Promise<MailpitAttachmentDataResponse>

      Attachment as binary data and the content type

      const message = await mailpit.getMessageSummary();
      if (message.Attachments.length) {
      const attachment = await mailpit.getMessageAttachment(message.ID, message.Attachments[0].PartID);
      // Do something with the attachment data
      }
    • Retrieves the headers of a specific message.

      Parameters

      • id: string = "latest"

        The message database ID. Defaults to latest to return the latest message.

      Returns Promise<MailpitMessageHeadersResponse>

      Message headers

      Header keys are returned alphabetically.

      const headers = await mailpit.getMessageHeaders();
      
    • Retrieves the full email message source as plain text.

      Parameters

      • id: string = "latest"

        The message database ID. Defaults to latest to return the latest message.

      Returns Promise<string>

      Plain text message source

      const messageSource = await mailpit.getMessageSource();
      
    • Retrieves a summary of a specific message and marks it as read.

      Parameters

      • id: string = "latest"

        The message database ID. Defaults to latest to return the latest message.

      Returns Promise<MailpitMessageSummaryResponse>

      Message summary

      const message = await mailpit.getMessageSummary();
      
    • Retrieves a list of all the unique tags.

      Returns Promise<string[]>

      All unique message tags

      const tags = await mailpit.getTags();
      
    • Performs an HTML check on a specific message.

      Parameters

      • id: string = "latest"

        The message database ID. Defaults to latest to return the latest message.

      Returns Promise<MailpitHTMLCheckResponse>

      The summary of the message HTML checker

      const htmlCheck = await mailpit.htmlCheck();
      
    • Performs a link check on a specific message.

      Parameters

      • id: string = "latest"

        The message database ID. Defaults to latest to return the latest message.

      • follow: boolean = false

        Whether to follow links. Defaults to false.

      Returns Promise<MailpitLinkCheckResponse>

      The summary of the message Link checker.

      const linkCheck = await mailpit.linkCheck();
      
    • Retrieves a list of message summaries ordered from newest to oldest.

      Parameters

      • start: number = 0

        The pagination offset. Defaults to 0.

      • limit: number = 50

        The number of messages to retrieve. Defaults to 50.

      Returns Promise<MailpitMessagesSummaryResponse>

      A list of message summaries

      Only contains the number of attachments and a snippet of the message body.

      getMessageSummary() for more attachment and body details for a specific message.

      const messages = await.listMessages();
      
    • Release a message via a pre-configured external SMTP server.

      Parameters

      • id: string

        The message database ID. Use latest to return the latest message.

      • relayTo: { To: string[] }

        Array of email addresses to relay the message to

      Returns Promise<string>

      Plain text "ok" response

      This is only enabled if message relaying has been configured.

      const message = await mailpit.releaseMessage("latest", ["user1@example.test", "user2@example.test"]);
      
    • Renames an existing tag.

      Parameters

      • tag: string

        The current name of the tag.

      • newTagName: string

        A new name for the tag.

      Returns Promise<string>

      Plain text "ok" response

      Tags are limited to the following characters: a-z, A-Z, 0-9, -, ., spaces, and _, and must be a minimum of 1 character. Other characters are silently stripped from the tag.

      await mailpit.renameTag("Old Tag Name", "New Tag Name");
      
    • Renders the HTML part of a specific message which can be used for UI integration testing.

      Parameters

      • id: string = "latest"

        The message database ID. Defaults to latest to return the latest message.

      • Optionalembed: 1

        Whether this route is to be embedded in an iframe. Defaults to undefined. Set to 1 to embed. The embed parameter will add target="_blank" and rel="noreferrer noopener" to all links. In addition, a small script will be added to the end of the document to post (postMessage()) the height of the document back to the parent window for optional iframe height resizing. Note that this will also transform the message into a full HTML document (if it isn't already), so this option is useful for viewing but not programmatic testing.

      Returns Promise<string>

      Rendered HTML

      Attached inline images are modified to link to the API provided they exist. If the message does not contain an HTML part then a 404 error is returned.

      const html = await mailpit.renderMessageHTML();
      
    • Renders just the message's text part which can be used for UI integration testing.

      Parameters

      • id: string = "latest"

        The message database ID. Defaults to latest to return the latest message.

      Returns Promise<string>

      Plain text

      const html = await mailpit.renderMessageText();
      
    • Sets and/or resets the Chaos triggers configuration (if enabled).

      Parameters

      • triggers: MailpitChaosTriggersRequest = {}

        The request containing the chaos triggers. Omitted triggers will reset to the default 0% probabibility.

      Returns Promise<MailpitChaosTriggersResponse>

      The updated Chaos triggers configuration

      This will return an error if Chaos is not enabled at runtime.

      // Reset all triggers to `0%` probability
      const triggers = await mailpit.setChaosTriggers();
      // Set `Sender` and reset `Authentication` and `Recipient` triggers
      const triggers = await mailpit.setChaosTriggers({ Sender: { ErrorCode: 451, Probability: 5 } });
    • Set the read status of messages.

      Parameters

      • readStatus: MailpitReadStatusRequest

        The request containing the message database IDs/search string and the read status.

        Request parameters for the setReadStatus() API.

        • OptionalIDs?: string[]

          Array of message database IDs

        • OptionalRead?: boolean

          Read status

          false
          
      • Optionalparams: MailpitTimeZoneRequest

        Optional parameters for defining the time zone when using the before: and after: search filters.

      Returns Promise<string>

      Plain text "ok" response

      You can optionally provide an array of IDs OR a Search filter. If neither is set then all messages are updated.

      // Set all messages as unread
      await mailpit.setReadStatus();

      // Set all messages as read
      await mailpit.setReadStatus({ Read: true });

      // Set specific messages as read using IDs
      await mailpit.setReadStatus({ IDs: ["1", "2", "3"], Read: true });

      // Set specific messages as read using search
      await mailpit.setReadStatus({ Search: "from:example.test", Read: true });

      // Set specific messages as read using after: search with time zone
      await mailpit.setReadStatus({ Search: "after:2025-04-30", Read: true }, { tz: "America/Chicago" });
    • Sets and removes tag(s) on message(s). This will overwrite any existing tags for selected message database IDs.

      Parameters

      • request: MailpitSetTagsRequest

        The request containing the message IDs and tags. To remove all tags from a message, pass an empty Tags array or exclude Tags entirely.

      Returns Promise<string>

      Plain text "ok" response

      Tags are limited to the following characters: a-z, A-Z, 0-9, -, ., spaces, and _, and must be a minimum of 1 character. Other characters are silently stripped from the tag.

      // Set tags on message(s)
      await mailpit.setTags({ IDs: ["1", "2", "3"], Tags: ["tag1", "tag2"] });
      // Remove tags from message(s)
      await mailpit.setTags({ IDs: ["1", "2", "3"]});
    • Performs a SpamAssassin check (if enabled) on a specific message.

      Parameters

      • id: string = "latest"

        The message database ID. Defaults to latest to return the latest message.

      Returns Promise<MailpitSpamAssassinResponse>

      The SpamAssassin summary (if enabled)

      const spamAssassinCheck = await mailpit.spamAssassinCheck();