Sending email in Go (Golang): libraries and code examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Sending email in Go (Golang): libraries and code examples



Sending email is one of the key tasks for any web application: user registration, password reset, notifications, and newsletters. The Go (Golang) language has both standard tools for working with SMTP and powerful third-party libraries that simplify sending emails with HTML templates, attachments, and support for various mail services. In this article, we will take a detailed look at how to send email in Go, review popular libraries, and provide practical code examples.



Standard library net/smtp



The net/smtp package is part of the Go standard library and provides a minimal set of functions for sending emails via an SMTP server. It is suitable for simple text messages but requires manual construction of headers and the email body in accordance with RFC 5322.



Example of sending a simple text email:



package main

import ( "fmt" "net/smtp")

func main() { // Data for connecting to the SMTP server smtpHost := "smtp.example.com" smtpPort := "587" from := "sender@example.com" password := "your_password" to := []string{"recipient@example.com"}

// Form the email body (must include headers) subject := "Subject: Hello from Go!" mime := "MIME-version: 1.0;Content-Type: text/plain; charset=\\"UTF-8\\";

" body := "This is a test email sent using Go." msg := []byte(subject + mime + body)

// Authentication auth := smtp.PlainAuth("", from, password, smtpHost)

// Sending err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, msg) if err != nil { fmt.Println("Error sending:", err) return } fmt.Println("Email sent successfully!")}


Limitations of net/smtp:

  • Does not support attachments without manual Base64 encoding.
  • Requires manual formatting of MIME headers.
  • Inconvenient for sending HTML emails with images.


Library gomail/v2



One of the most popular third-party libraries for sending email in Go is gomail (package gopkg.in/mail.v2). It provides a convenient API for working with emails: support for attachments, HTML and text parts, SMTP connection configuration, and the ability to send via SSL/TLS.



Installation:

go get gopkg.in/mail.v2


Example of sending an HTML email with an attachment:



package main

import ( "fmt" "gopkg.in/mail.v2")

func main() { m := mail.NewMessage() m.SetHeader("From", "sender@example.com") m.SetHeader("To", "recipient@example.com") m.SetHeader("Subject", "HTML email with attachment")

// Alternative parts: text and HTML m.SetBody("text/plain", "If you see this text, your client does not support HTML.") m.AddAlternative("text/html", "<h1>Hello!</h1><p>This is the HTML version of the email.</p>")

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

// Configure SMTP client d := mail.NewDialer("smtp.example.com", 587, "sender@example.com", "your_password") d.StartTLSPolicy = mail.MandatoryStartTLS

if err := d.DialAndSend(m); err != nil { fmt.Println("Error:", err) return } fmt.Println("Email sent!")}


Advantages of gomail:

  • Simple and intuitive API.
  • Automatic generation of MIME headers.
  • Support for attachments and embedded images (CID).
  • Works with SSL/TLS and StartTLS.


Library go-smtp (emersion/go-smtp)



The go-smtp library by emersion is a lower-level alternative that gives you full control over the SMTP connection. It is useful if you

Recommendations