How to send test email in Go

Introduction - sending email in Go

If you're writing software using Go, you're probably sending emails, either as part of sign up flows, or for sending notifications or maybe you just like sending pictures of Chameleons!

The standard way to send email is via an SMTP server, usually hosted by a specialist email service provider but can also be hosted yourself.

Sending email in Go is simple with the net/smtp package. The Go SMTP client automatically negotiates an encrypted connection using STARTTLS when using SendMail, which is compatible with Imitate Email and ensures that your credentials are encrypted.

The following code illustrates how to send an email:

package main

import (
  "log"
  "net/smtp"
)

func main() {
  // hostname is used by PlainAuth to validate the TLS certificate.
  hostname := "smtp.imitate.email"
  auth := smtp.PlainAuth("", "username", "password", hostname)

  msg := `To: to@example.net
From: from@example.com
Subject: Imitate Email using GoLang

This is the message content!
Thanks
`
  err := smtp.SendMail(hostname+":587", auth, "from@example.com", []string{"to@example.net"},
    []byte(msg))
  if err != nil {
    log.Fatal(err)
  }
}

Sending Test Email: Set up Imitate Email

If you've not already done so please sign up to Imitate Email.

For Imitate Email, when sending test emails you use the following server details:

Host: smtp.imitate.email
Port: 587

For your username and password you need to decide if you're using your personal mailbox or have set up a project in Imitate Email.

Locate your mailbox credentials

Your username

To locate your username go to Settings > My Mailbox where you will find it (it is the same for all mailboxes in your account)

For the password

If you're on the Free or Developer plan or you're setting this up for your personal mailbox copy the password from the same place as the username.

If you're setting this up for a team mailbox go to Settings > Projects and you will find the password next to the symbol for your mailbox. It will look like the following 6bcb69b2-08ac-4c67-911a-10442f7d84b3

Screenshot of Imitate Email personal settings

That's it!