Send emails with SMTP in Golang with net/smtp and gomail


SMTP in Go

The Go standard library provides the essential (and low-level) means for working with SMTP in the net/smtp package. That being said, working with it is quite verbose and can get tricky especially if you plan to add custom email headers and attachments.

For sending more complex emails (e.g., with attachments or embedded images), you might consider using a third-party library like gomail or others available in the Go ecosystem. These libraries provide a more convenient API for constructing and sending emails with various features.

In this guide, we'll go through sending emails with SMTP using net/smtp and gomail libraries.

Prerequisites

Sending with the net/smtp package

The net/smtp package provides the SendMail function, which you can use to send an email. Here's a basic example that sends a simple email:

package main
import (
    "net/smtp"
)
func main() {
    // Set up authentication information.
    auth := smtp.PlainAuth("", "YOUR_SMTP_USERNAME", "YOUR_SMTP_PASSWORD", "send.ahasend.com")
    // Connect to the server, authenticate, set the sender and recipient,
    // and send the email all in one step.
    to := []string{"[email protected]"}
    msg := []byte("To: [email protected]\r\n" +
        "Subject: Hello there!\r\n" +
        "\r\n" +
        "This is the email body.\r\n")
    err := smtp.SendMail("send.ahasend.com:587", auth, "[email protected]", to, msg)
    if err != nil {
        panic(err)
    }
}
Replace [email protected], YOUR_SMTP_USERNAME and YOUR_SMTP_PASSWORD with an email address from your domain (that is added to your AhaSend account), and your actual SMTP credential username and password.

To send an HTML email, you need to modify the msg variable to specify that the content type is HTML:

msg := []byte("To: [email protected]\r\n" +
    "Subject: Hello there!\r\n" +
    "Content-Type: text/html; charset=UTF-8\r\n\r\n" +
    "<html><body><strong>This is bold text in an HTML email</strong></body></html>\r\n")

The net/smtp package does not natively support sending emails with attachments. To send attachments, you would typically need to construct a MIME multipart message manually or use a third-party library like gomail that simplifies working with MIME emails.

Sending with the gomail.v2 package

Using gomail.v2 in Go is a straightforward and efficient way to send emails via SMTP. gomail.v2 is a package that provides a simple API for sending emails and supports attachments, HTML bodies, and custom headers.

You can install gomail.v2 by running the following command in your terminal:

go get gopkg.in/gomail.v2

Here's a simple example of how to send an email with a subject and plaintext body using gomail.v2.

package main
import (
    "gopkg.in/gomail.v2"
)
func main() {
    m := gomail.NewMessage()
    m.SetHeader("From", "[email protected]")
    m.SetHeader("To", "[email protected]")
    m.SetHeader("Subject", "Hello!")
    m.SetBody("text/plain", "This is the plain text body of the email.")
    d := gomail.NewDialer("send.ahasend.com", 587, "YOUR_SMTP_USERNAME", "YOUR_SMTP_PASSWORD")
    if err := d.DialAndSend(m); err != nil {
        panic(err)
    }
}
Replace [email protected], YOUR_SMTP_USERNAME and YOUR_SMTP_PASSWORD with an email address from your domain (that is added to your AhaSend account), and your actual SMTP credential username and password.

To send an email that includes HTML content, simply change the body's content type to text/html.

m.SetBody("text/html", "<strong>This is the HTML body of the email.</strong>")

Adding Attachments to the email mesasge

You can add attachments to your email with the Attach method.

m.Attach("/path/to/file.pdf")

Adding email headers

You can add custom headers multiple recipients, CC, and BCC in your email.

// Set the To header.
m.SetHeader("To", "[email protected]", "[email protected]")
// Set email address header for CC and BCC
m.SetAddressHeader("Cc", "[email protected]", "CC Name")
m.SetAddressHeader("Bcc", "[email protected]", "BCC Name")
// Set custom header
m.SetHeader("AhaSend-Track-Opens", "true")
m.SetHeader("AhaSend-Track-Clicks", "true")

gomail.v2 is a powerful library for sending emails in Go, supporting a wide range of features from simple plaintext emails to complex messages with HTML content and attachments. This guide covered the basics to get you started. For more advanced features and options, refer to the gomail.v2 documentation.

AhaSend
Send up to 1,000 emails per month on us, no credit card required!