https://github.com/mailslurp/examples
package main
//<gen>go_send_email_deps
import (
"context"
"github.com/antihax/optional"
sasl "github.com/emersion/go-sasl"
smtp "github.com/emersion/go-smtp"
mailslurp "github.com/mailslurp/mailslurp-client-go"
"github.com/stretchr/testify/assert"
"log"
"os"
"strings"
"testing"
)
//</gen>
//<gen>go_send_email_get_client
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
}
//</gen>
// how to send insecurely with mailslurp
//<gen>go_send_email_insecure
func Test_CanSendEmail_Insecure(t *testing.T) {
// create a context with your api key
client, ctx := getMailSlurpClient(t)
// create an inbox using the inbox controller
opts := &mailslurp.CreateInboxOpts{
InboxType: optional.NewString("SMTP_INBOX"),
}
// create two inboxes for testing
inbox1, _, _ := client.InboxControllerApi.CreateInbox(ctx, opts)
//<gen>golang_smtp_access
smtpAccess, _, _ := client.InboxControllerApi.GetImapSmtpAccess(ctx, &mailslurp.GetImapSmtpAccessOpts{
InboxId: optional.NewInterface(inbox1.Id),
})
inbox2, _, _ := client.InboxControllerApi.CreateInbox(ctx, opts)
// create a plain auth client with smtp access details
auth := sasl.NewPlainClient("", smtpAccess.SmtpUsername, smtpAccess.SmtpPassword)
// dial connection to the smtp server
c, err := smtp.Dial("mx.mailslurp.com:2525")
assert.NoError(t, err, "Expect client dial")
defer c.Close()
// issue hello smtp command
log.Println("Say hello")
err = c.Hello("test")
assert.NoError(t, err, "Expect hello")
// issue auth smtp command
log.Println("Set auth")
err = c.Auth(auth)
assert.NoError(t, err, "Expect auth")
// send the email
log.Println("Send email")
to := []string{inbox2.EmailAddress}
msg := strings.NewReader("To: " + inbox2.EmailAddress + "\r\n" +
"Subject: Hello Insecure Gophers!\r\n" +
"\r\n" +
"This is the email body.\r\n")
err = c.SendMail(inbox1.EmailAddress, to, msg)
assert.NoError(t, err, "Expect insecure smtp send to work")
//</gen>
// fetch the email for inbox2
log.Println("Wait for email to arrive")
waitOpts := &mailslurp.WaitForLatestEmailOpts{
InboxId: optional.NewInterface(inbox2.Id),
Timeout: optional.NewInt64(30000),
UnreadOnly: optional.NewBool(true),
}
email, _, err := client.WaitForControllerApi.WaitForLatestEmail(ctx, waitOpts)
// assert email contents
log.Println("Email received: " + *email.Subject)
assert.NoError(t, err)
assert.Contains(t, *email.Subject, "Hello Insecure Gophers")
assert.Contains(t, *email.Body, "This is the email body")
}
//</gen>
// send using TLS
//<gen>go_send_email_tls
func Test_CanSendEmail_TLS(t *testing.T) {
// create a context with your api key
client, ctx := getMailSlurpClient(t)
// create an inbox using the inbox controller
opts := &mailslurp.CreateInboxOpts{
InboxType: optional.NewString("SMTP_INBOX"),
}
// create two inboxes for testing
inbox1, _, _ := client.InboxControllerApi.CreateInbox(ctx, opts)
smtpAccess, _, _ := client.InboxControllerApi.GetImapSmtpAccess(ctx, &mailslurp.GetImapSmtpAccessOpts{
InboxId: optional.NewInterface(inbox1.Id),
})
inbox2, _, _ := client.InboxControllerApi.CreateInbox(ctx, opts)
// send email from inbox1 to inbox2
auth := sasl.NewPlainClient("", smtpAccess.SmtpUsername, smtpAccess.SmtpPassword)
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
to := []string{inbox2.EmailAddress}
msg := strings.NewReader("To: " + inbox2.EmailAddress + "\r\n" +
"Subject: Hello Gophers!\r\n" +
"\r\n" +
"This is the email body.\r\n")
// not TLS mailslurp uses a different host
err := smtp.SendMail("mailslurp.mx:587", auth, inbox1.EmailAddress, to, msg)
if err != nil {
log.Fatal(err)
assert.NoError(t, err, "Expect smtp send to work")
}
// 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 Gophers")
assert.Contains(t, *email.Body, "This is the email body")
}
//</gen>
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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-sasl v0.0.0-20220912192320-0145f2c60ead h1:fI1Jck0vUrXT8bnphprS1EoVRe2Q5CKCX8iDlpqjQ/Y=
github.com/emersion/go-sasl v0.0.0-20220912192320-0145f2c60ead/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-smtp v0.16.0 h1:eB9CY9527WdEZSs5sWisTmilDX7gG+Q/2IdRcmubpa8=
github.com/emersion/go-smtp v0.16.0/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
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-20221215115134-d068f7544398 h1:YiGBlzg57UTRF6xSstDqrTz/KYxBE0sqp1e+ofjpM8s=
github.com/mailslurp/mailslurp-client-go v0.0.0-20221215115134-d068f7544398/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/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
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/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/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
module github.com/mailslurp/examples-golang-smtp
go 1.19
require (
github.com/antihax/optional v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emersion/go-sasl v0.0.0-20220912192320-0145f2c60ead // indirect
github.com/emersion/go-smtp v0.16.0 // indirect
github.com/golang/protobuf v1.2.0 // indirect
github.com/mailslurp/mailslurp-client-go v0.0.0-20221215115134-d068f7544398 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.1 // 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.1 // indirect
)
-include ../.env
test:
API_KEY=$(API_KEY) go test