Sending Emails in Go: A Comprehensive Guide

Emails are an essential part of any application, allowing you to communicate with users, send notifications, and more. In this article, we’ll explore two ways to send emails in Go: using the built-in smtp package and a third-party email package.

Using the smtp Package

The smtp package is part of the Go standard library, making it easy to get started. To use it, you’ll need to import the following packages:

go
import (
"crypto/tls"
"fmt"
"log"
"net/smtp"
)

Next, declare variables for the email’s content:

go
var (
from = "[email protected]"
password = "your-password"
to = "[email protected]"
host = "smtp.gmail.com"
port = 587
subject = "Test Email"
body = "This is a test email sent using Go."
)

Create a map to combine the email’s data:

go
headerMap := map[string]string{
"From": from,
"To": to,
"Subject": subject,
}

Use the PlainAuth method to authenticate the email:

go
auth := smtp.PlainAuth("", from, password, host)

Set up the TLS configuration and dial a connection:

go
tlsConfig := &tls.Config{ServerName: host}
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", host, port), tlsConfig)
if err != nil {
log.Fatal(err)
}

Create a client instance and authenticate:

go
client, err := smtp.NewClient(conn, host)
if err != nil {
log.Fatal(err)
}
if err := client.Auth(auth); err != nil {
log.Fatal(err)
}

Finally, write the email to the client and close the connection:

go
writer, err := client.Data()
if err != nil {
log.Fatal(err)
}
_, err = writer.Write([]byte(body))
if err != nil {
log.Fatal(err)
}
err = writer.Close()
if err != nil {
log.Fatal(err)
}
err = client.Quit()
if err != nil {
log.Fatal(err)
}

Using the email Package

The email package is a third-party library that provides a simpler way to send emails. To use it, install the package using the following command:

bash
go get github.com/jordan-wright/email

Import the package:

go
import (
"github.com/jordan-wright/email"
"fmt"
"log"
)

Declare variables for the email’s content:

go
var (
from = "[email protected]"
password = "your-password"
to = "[email protected]"
host = "smtp.gmail.com"
port = 587
subject = "Test Email"
body = "This is a test email sent using Go."
)

Create an email instance:

go
e := email.NewEmail()
e.From = from
e.To = []string{to}
e.Subject = subject
e.Text = []byte(body)

Send the email:

go
err := e.Send(fmt.Sprintf("%s:%d", host, port), smtp.PlainAuth("", from, password, host))
if err != nil {
log.Fatal(err)
}

Comparison

| Package | Pros | Cons |
| — | — | — |
| smtp | Built-in, customizable | Complex, requires manual authentication |
| email | Simple, easy to use | Third-party dependency, less customizable |

Choose the smtp package if you need more control over the email sending process or want to avoid third-party dependencies. Choose the email package if you want a simple and easy-to-use solution for sending emails.

By following this guide, you should be able to send emails in Go using either the smtp or email package. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *