Golang mail Library (SMTP)

How to send and receive emails in Go (test email addresses).

MailSlurp has an official Go library hosted on GitHub. You can use this library to call the MailSlurp API in Go!

Let's use MailSlurp to test email related processes using the popular github.com/stretchr/testify library for assertions.


First add MailSlurp:

go get github.com/mailslurp/mailslurp-client-go

We will also add test libraries and context:

go get github.com/stretchr/testify/assert
go get golang.org/x/oauth2
go get golang.org/x/net/context
go get github.com/antihax/optional

Import and configure

Now create a test file called main_test.go. Import and configure MailSlurp in your test like so:

package main

import (

var apiKey = "your-mailslurp-client"

func getMailSlurpClient() (*mailslurp.APIClient, context.Context) {

	// create a context with your api key
	ctx := context.WithValue(context.Background(), mailslurp.ContextAPIKey, mailslurp.APIKey{ Key: apiKey })

	// create mailslurp client
	config := mailslurp.NewConfiguration()
	client := mailslurp.NewAPIClient(config)

	return client, ctx

We can now create MailSlurp API clients using

client, ctx:= getMailSlurpClient()

Create inboxes

MailSlurp inboxes can be created on demand and have an ID and an email address.

Let's see how you create one:

func Test_CanCreateInbox(t *testing.T) {
	// create a context with your api key
	client, ctx := getMailSlurpClient(t)

	// create an inbox using the inbox controller
	opts := &mailslurp.CreateInboxOpts{}
	inbox, response, err := client.InboxControllerApi.CreateInbox(ctx, opts)

	assert.NoError(t, err)
	assert.Equal(t, response.StatusCode, 201)
	assert.Contains(t, inbox.EmailAddress, "@mailslurp.com")

Send emails

You can send emails in Go tests by first creating an inbox and then using the InboxControllerApi.SendEmail method:

func Test_CanSendEmail(t *testing.T) {
	// create a context with your api key
	client, ctx := getMailSlurpClient(t)

	// create an inbox we can send email from
	inbox, _, _ := client.InboxControllerApi.CreateInbox(ctx, nil)

	// send email from inbox
	sendEmailOptions := mailslurp.SendEmailOptions{
		To: []string{inbox.EmailAddress},
		Subject: "Test email",
		Body: "<h1>MailSlurp supports HTML</h1>",
		IsHTML: true,
	opts := &mailslurp.SendEmailOpts{
		SendEmailOptions: optional.NewInterface(sendEmailOptions),
	res, err := client.InboxControllerApi.SendEmail(ctx, inbox.Id, opts)

	assert.Equal(t, res.StatusCode, 201)

Receive emails (and extract content)

MailSlurp is unique in allowing you to receive emails in Go. No MailServer is required. First create an inbox then use the waitFor methods to wait for unread emails to arrive.

func Test_CanReceiveEmail(t *testing.T) {
	// create a context with your api key
	client, ctx := getMailSlurpClient(t)

	// create two inboxes for testing
	inbox1, _, _ := client.InboxControllerApi.CreateInbox(ctx, nil)
	inbox2, _, _ := client.InboxControllerApi.CreateInbox(ctx, nil)

	// send email from inbox1 to inbox2
	sendEmailOptions := mailslurp.SendEmailOptions{
		To: []string{inbox2.EmailAddress},
		Subject: "Hello inbox2",
		Body: "Your code is: 123",
	sendOpts := &mailslurp.SendEmailOpts{
		SendEmailOptions: optional.NewInterface(sendEmailOptions),
	res, err := client.InboxControllerApi.SendEmail(ctx, inbox1.Id, sendOpts)

	assert.Equal(t, res.StatusCode, 201)

You can receive emails using the WaitForControllerApi:

	// fetch the email for inbox2
	waitOpts := &mailslurp.WaitForLatestEmailOpts{
		InboxId: optional.NewInterface(inbox2.Id),
		Timeout: optional.NewInt64(30000),
		UnreadOnly: optional.NewBool(true),
	email, _, err := client.WaitForControllerApi.WaitForLatestEmail(ctx, waitOpts)
	assert.Contains(t, email.Subject, "Hello inbox2")
	assert.Contains(t, email.Body, "Your code is")

	// can extract the contents
	r := regexp.MustCompile(`Your code is: ([0-9]{3})`)
	code := r.FindStringSubmatch(email.Body)[1]
	assert.Equal(t, code, "123")

Next steps

See the source code for full method documentation.

Related content

Golang email library

Golang Email Library for sending and receiving emails in Go over SMTP or HTTP/S.

Email for testing

Test email accounts for email testing. Alternatives to Mailinator, MailTrap, Mailosaur and more.

How to wait for Selenium to start during Codeception tests

Example tutorial for how to wait until webdriver and Selenium have started during Codeception PHP tests

Email API for email marketing and more

APIs for email marketing and social campaign testing. Send, receive, validate and test emails in code and online.

How to test an email address

Test email accounts for testing email addresses in code or online. Create fake email accounts for testing.

How to start selenium in a background process and wait for i...

Spawn Selenium server process before tests start for easier acceptance testing.

Ready to dive in?Start building email applications today.