Working with Email in Go

12.03.2021

Intro

Go provides an smtp package which uses the SMTP standard to send email. Sending email is a common use in most programs. In practice, you will often want to use a service such as Mailgun and connect to its SMTP server. In this article, we will learn how to send Email in Go.

SendMail

The first function we will use is SendMail It allows to connect and send email. For this tutorial, we will use a fake Gmail account, although in practice, Gmail limits the emails you can send, so you want to use a different service or your own SMTP server.

We first need a few information for the function. We will start by creating the following variables.

from := "test@gmail.com"

user := "gmailSmtpUser"
pass := "gmailSmtpPass"

to := []string { "someone@somewhere.com" }

Next, we want to set up the SMTP host and our message.

addr := "smtp.mailtrap.io:2525"
host := "smtp.mailtrap.io"

msg := []byte(
	fmt.Sprintf("From: %s \r\n", from) +
	fmt.Sprintf("To: %s \r\n", to) +
	"Subject: This is a message from Go!\r\n\r\n" +
	"Hello and Stuff! \r\n",
)

Finally, we create an SMTP auth and send the email.

auth := smtp.PlainAuth("", user, pass, host)

err := smtp.SendMail(addr, auth, from, to, msg)

if err != nil {
	log.Fatal(err)
}

Using Other Email Features

The above uses a basic plain text email, but we can modify the email to do more. We simply follow the standard email format.

If we want to use html, we can add the html header and then add html to the body.

 msg := []byte(
	"MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\r\n", + 
	fmt.Sprintf("From: %s \r\n", from) +
	fmt.Sprintf("To: "%s \r\n", to) +
	"Subject: This is a message from Go!\r\n\r\n" +
	"<h1>Hello!</h1> \r\n"
)

We can also add Carbon Copy and Blind Carbon Copy, CC and BCC:

 msg := []byte(
	"MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\r\n", + 
	fmt.Sprintf("From: %s \r\n", from) +
	fmt.Sprintf("To: "%s \r\n", to) +
	"Cc: other@other.com, other2@other.com" + 
	"Bcc: other3@other.com, other4@other.com" + 
	"Subject: This is a message from Go!\r\n\r\n" +
	"<h1>Hello!</h1> \r\n"
)

There are a few other things you may want to do such as attach a file. I would suggest starting off with a library such as https://github.com/jordan-wright/email.

e := NewEmail()
e.AttachFile("test.txt")
e.Send("smtp.gmail.com:587", smtp.PlainAuth("", "test@gmail.com", "password123", "smtp.gmail.com"))

Full Example

You can run the full example code by copying the code below to a file called main.go and run go run main.go

package main

import (
	"fmt"
	"log"
	"net/smtp"
)

func main() {
	from := "test@gmail.com"

	user := "gmailSmtpUser"
	pass := "gmailSmtpPass"

	to := []string{"someone@somewhere.com"}

	addr := "smtp.mailtrap.io:2525"
	host := "smtp.mailtrap.io"

	msg := []byte(
		fmt.Sprintf("From: %s \r\n", from) +
			fmt.Sprintf("To: %s \r\n", to) +
			"Subject: This is a message from Go!\r\n\r\n" +
			"Hello and Stuff! \r\n",
	)

	auth := smtp.PlainAuth("", user, pass, host)

	err := smtp.SendMail(addr, auth, from, to, msg)

	if err != nil {
		log.Fatal(err)
	}
}