Golang email library
Golang Email Library for sending and receiving emails in Go over SMTP or HTTP/S.
Generate test email accounts and real inboxes in Golang using MailSlurp SDKs. Free for personal use, send and receive emails and attachments in Go code and tests. MailSlurp's free email API enables teams and developers to create email mailboxes on demand then control email accounts in code and applications.
Get started
MailSlurp has an official Golang package and support for Go SMTP clients.
Installing
Install the MailSlurp golang package from github using go get and go mod.
go get github.com/mailslurp/mailslurp-client-go
You may also need these dependencies:
go get golang.org/x/oauth2
go get golang.org/x/net/context
go get github.com/antihax/optional
To configure the MailSlurp client obtain a free API Key. Then import the libraries:
import (
"context"
"github.com/mailslurp/mailslurp-client-go"
)
Then configure the client:
var apiKey = "your-mailslurp-client"
// 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)
Example
Here is a short example using stretchr/testify
and go test
to create email addresses, send emails and receive them.
package main
import (
"context"
"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)
// 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
}
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")
}
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.NoError(t,err)
assert.Equal(t, res.StatusCode, 201)
}
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
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")
}
Related content
CSharp Email API and SMTP library
Receive email in DotNET Core using C# and MailSlurp
Golang email library for sending and reading emails
Golang Email Library for sending and receiving emails in Go over SMTP or HTTP/S.
PHP
PHP email API for creating inboxes, sending email, and receiving attachments in code and tests.
Ruby Mailer SDK - send and receive emails in Rails, Rspec and more using MailSlurp's official email Gem
Receive email and attachments with Rails, Rspec, and Ruby without SMTP using MailSlurp Ruby Gem.
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.
Mailinator alternative
Alternatives to Mailinator for test email accounts. Create real email addresses using MailSlurp
How to start selenium in a background process and wait for it to start
Spawn Selenium server process before tests start for easier acceptance testing.
CypressJS Example
Test email sign-up. password verification and more with Cypress JS and MailSlurp.
CypressJS Email Testing
Use real email accounts in CypressJS to test user sign-up, email verification, and more.
Golang mail Library (SMTP)
How to send and receive emails in Go (test email addresses).
Java JVM Examples
Test email sending and receive emails without a mail server.
TestNG Selenium Java Example
Testing user sign up in Java using TestNG and MailSlurp test email accounts
Codeception PHP acceptance testing using real email address APIs
Write acceptance tests in PHP with real email addresses using Codeception and MailSlurp
PHP Email Test Plugins: send and receive email in PHPUnit (example code)
How to send and receive emails in PHPUnit tests.
PyTest Email Testing
Send and receive email in Pytest Python tests.
Java, Selenium
Receive emails in Java test suites using MailSlurp, Junit, and Selenium.
Receive email in PHP: using MailSlurp to send and receive emails
Test email in PHP using real email addresses
Testing authentication using real email addresses in Ruby with Capybara, Cucumber, and Selenium
Cucumber example project using Capybara to test user authentication using real email addresses.
Test applications with real emails using Serenity BDD, JBehave and Selenium
Email acceptance testing with Serenity and MailSlurp. Test applications with real email addresses.
Specflow user sign-up testing with MailSlurp accounts
How to test .NET authentication and sign-up using real email accounts with MailSlurp and SpecFlow.
Jest, Puppeteer
Test email accounts in React with Jest and Puppeteer. Send and receive emails in Javascript.
.NET Selenium C#
Send and receive email in DotNET Nunit tests using Selenium and MailSlurp.
Cucumber, Ruby
Generate test email accounts with Ruby and Cucumber. Test email sign-up, password verification and more.
Webdriver, JS, WDIO
Test email related processes like sign-up and verification using WDIO WebDriver and MailSlurp.
TestCafe end-to-end MFA testing for user sign-up and email verification
End-to-end testing with MailSlurp, NodeJS, and TestCafe.
How To Test Emails Before You Send
There are many free tools to test emails before sending. This can help prevent spam warnings and increase deliverability.
GraphQL Email API
Fetch and read emails with GraphQL inboxes using MailSlurp email API.
Testing OTP password link username and password for 2 factor authentication (2FA)
Testing OTP password link username and password for 2 factor authentication (2FA)
Test email address
Free test email address for testing emails online with web dashboard or REST API.
How to test 2FA OTP login using SMS codes with Playwright
The ultimate guide to testing OAuth one-time-password flows with real SMS MFA. Use Playwright to automate authentication tests with programmable TXT message APIs.
Testing guide
Integration testing with disposable email accounts using CypressJS, Selenium and many other frameworks. Test OTP password login, transactional emails, notifications and more.
Testing email with Cypress test email accounts
Test email accounts for CypressJS. End-to-end testing with real email addresses using MailSlurp Cypress plugin.
Testing Webhooks
How to test HTTP webhooks using MailSlurp test hooks.
Testing Email with Cypress JS and MailSlurp
Email testing with Cypress JS