mailslurp-examples - golang-email-test

https://github.com/mailslurp/examples

Table of Contents

golang-email-test/main_test.go

package main

import (
	"context"
	"fmt"
	"github.com/antihax/optional"
	mailslurp "github.com/mailslurp/mailslurp-client-go"
	"github.com/stretchr/testify/assert"
	"os"
	"regexp"
	"testing"
)

var apiKey = os.Getenv("API_KEY")

func getMailSlurpClient(t *testing.T) (*mailslurp.APIClient, context.Context) {
	assert.NotNil(t, apiKey)

	//<gen>golang_init_client
	// go get github.com/mailslurp/mailslurp-client-go
	// 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)
	//</gen>

	return client, ctx
}

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

	//<gen>golang_create_inbox
	// 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.net")
	//</gen>
}

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)

	//<gen>golang_send_email
	// 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.NoError(t, err)
	assert.Equal(t, res.StatusCode, 201)
	//</gen>
}

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.NoError(t, err)
	assert.Equal(t, res.StatusCode, 201)

	// fetch the email for inbox2
	//<gen>golang_wait_for_email
	waitOpts := &mailslurp.WaitForLatestEmailOpts{
		InboxId:    optional.NewInterface(inbox2.Id),
		Timeout:    optional.NewInt64(30000),
		UnreadOnly: optional.NewBool(true),
	}
	email, _, err := client.WaitForControllerApi.WaitForLatestEmail(ctx, waitOpts)
	assert.NoError(t, err)
	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")
	//</gen>

	//<gen>golang_list_and_delete
	// can list emails
	listOpts := &mailslurp.GetInboxEmailsPaginatedOpts{
		Page: optional.NewInt32(0),
		Size: optional.NewInt32(10),
		Sort: optional.NewString("DESC"),
	}
	emails, _, err := client.InboxControllerApi.GetInboxEmailsPaginated(ctx, inbox2.Id, listOpts)
	assert.NoError(t, err)
	fmt.Println(emails.Content[0].Subject)
	//</gen>
}

golang-email-test/go.sum

cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/mailslurp/mailslurp-client-go v0.0.0-20211009062827-152032823491 h1:9Lo5nD9weeMafbQsbrsJ98hUNM9cFjOcdVipA5lW3do=
github.com/mailslurp/mailslurp-client-go v0.0.0-20211009062827-152032823491/go.mod h1:t/C8qFZGrjNydNjr3Fl1xQjvcxqhmgjoDM5aZevi/Sw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

golang-email-test/go.mod

module mailslurp.com/examples/golang/v2

go 1.17

require (
	github.com/antihax/optional v1.0.0
	github.com/mailslurp/mailslurp-client-go v0.0.0-20211009062827-152032823491
	github.com/stretchr/testify v1.7.0
)

require (
	github.com/davecgh/go-spew v1.1.0 // indirect
	github.com/golang/protobuf v1.2.0 // indirect
	github.com/pmezard/go-difflib v1.0.0 // indirect
	golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e // indirect
	golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect
	google.golang.org/appengine v1.4.0 // indirect
	gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)

golang-email-test/README.md

# MailSlurp Go example
See [MailSlurp Go Docs](https://www.mailslurp.com/docs/go/) for more information.

## Setup
```bash
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
go get github.com/mailslurp/mailslurp-client-go
```

## Run example
```
API_KEY="your-api-key" go test
```

golang-email-test/Makefile

-include ../.env

test:
	API_KEY=$(API_KEY) go test