Skip to content

Inbucket

Since v0.28.0

Introduction

The Testcontainers module for Inbucket.

Adding this module to your project dependencies

Please run the following command to add the Inbucket module to your Go dependencies:

go get github.com/testcontainers/testcontainers-go/modules/inbucket

Usage example

ctx := context.Background()

inbucketContainer, err := inbucket.Run(ctx, "inbucket/inbucket:sha-2d409bb")
defer func() {
    if err := testcontainers.TerminateContainer(inbucketContainer); err != nil {
        log.Printf("failed to terminate container: %s", err)
    }
}()
if err != nil {
    log.Printf("failed to start container: %s", err)
    return
}

Send and list messages with the Inbucket client

Install the client used by this example:

go get github.com/inbucket/[email protected]+incompatible

The example lists an empty mailbox, delivers a message over SMTP, and lists the mailbox again.

ctx := context.Background()
ctr, err := inbucket.Run(ctx, "inbucket/inbucket:sha-2d409bb")
defer func() {
    if err := testcontainers.TerminateContainer(ctr); err != nil {
        log.Printf("failed to terminate container: %s", err)
    }
}()
if err != nil {
    log.Printf("failed to start container: %s", err)
    return
}

smtpURL, err := ctr.SmtpConnection(ctx)
if err != nil {
    log.Printf("failed to get SMTP connection: %s", err)
    return
}
webURL, err := ctr.WebInterface(ctx)
if err != nil {
    log.Printf("failed to get web interface: %s", err)
    return
}
restClient, err := client.New(webURL)
if err != nil {
    log.Printf("failed to create Inbucket client: %s", err)
    return
}

headers, err := restClient.ListMailbox("[email protected]")
if err != nil {
    log.Printf("failed to list empty mailbox: %s", err)
    return
}
fmt.Printf("Messages before delivery: %d\n", len(headers))

msg := []byte("To: [email protected]\r\n" +
    "Subject: Testcontainers test!\r\n" +
    "\r\n" +
    "This is a Testcontainers test.\r\n")
if err := smtp.SendMail(smtpURL, nil, "[email protected]", []string{"[email protected]"}, msg); err != nil {
    log.Printf("failed to send message: %s", err)
    return
}
headers, err = restClient.ListMailbox("[email protected]")
if err != nil {
    log.Printf("failed to list mailbox after delivery: %s", err)
    return
}
fmt.Printf("Messages after delivery: %d\n", len(headers))

Module Reference

Run function

Info

The RunContainer(ctx, opts...) function is deprecated and will be removed in the next major release of Testcontainers for Go.

The Inbucket module exposes one entrypoint function to create the Inbucket container, and this function receives three parameters:

func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*InbucketContainer, error)
  • context.Context, the Go context.
  • string, the Docker image to use.
  • testcontainers.ContainerCustomizer, a variadic argument for passing options.

Image

Use the second argument in the Run function to set a valid Docker image. In example: Run(context.Background(), "inbucket/inbucket:sha-2d409bb").

Container Options

When starting the Inbucket container, you can pass options in a variadic way to configure it.

The following options are exposed by the testcontainers package.

Basic Options

Lifecycle Options

Files & Mounts Options

Build Options

Logging Options

Image Options

Networking Options

Advanced Options

Experimental Options

Container Methods

The Inbucket container exposes the following methods:

SmtpConnection

This method returns the connection string to connect to the Inbucket container SMTP service, using the 2500 port.

smtpURL, err := ctr.SmtpConnection(ctx)

WebInterface

This method returns the connection string to connect to the Inbucket container web interface, using the 9000 port.

webInterfaceURL, err := ctr.WebInterface(ctx)