gomail

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

gomail library for sending Email in Go

gomail is a powerful and easy-to-use library in Go (Golang) for sending email. It supports HTML emails, attachments, multiple recipients, SMTP authentication, and TLS/SSL encryption. The library is ideal for developers who need to integrate email notifications, registration confirmations, or marketing newsletters into their Go applications.

Why is gomail needed?

The standard Go library (net/smtp) provides basic capabilities for sending emails, but it often lacks functionality for real-world tasks: it is difficult to work with HTML templates, attachments, multiple recipients, and modern security requirements (STARTTLS, OAuth2). gomail solves these problems by offering a clean API and flexible configuration.

Installation

To install gomail, run the command:

go get github.com/go-mail/mail/v2

Main features

  • Sending via SMTP with TLS/SSL support
  • Support for HTML and plain text emails
  • File attachments and embedded images
  • Multiple recipients (To, Cc, Bcc)
  • Customizable headers and encodings
  • OAuth2 support (e.g., Gmail API)

Go code example

Below is a simple example of sending an HTML email via the Gmail SMTP server:

package main

import (\t"crypto/tls"\t"fmt"\t"log"

\tgomail "github.com/go-mail/mail/v2")

func main() {\td := gomail.NewDialer("smtp.gmail.com", 587, "your_email@gmail.com", "your_app_password")\td.TLSConfig = &tls.Config{InsecureSkipVerify: false}

\tm := gomail.NewMessage()\tm.SetHeader("From", "your_email@gmail.com")\tm.SetHeader("To", "recipient@example.com")\tm.SetHeader("Subject", "Hello from Go!")\tm.SetBody("text/html", "<h1>Hello!</h1><p>This is a test email.</p>")\tm.Attach("/path/to/file.pdf")

\tif err := d.DialAndSend(m); err != nil {\t\tlog.Fatal(err)\t}\tfmt.Println("Email sent successfully!")}

When to use gomail

  • When you need to send transactional emails (confirmations, password reset)
  • For creating a mailing service in Go
  • When attachments or complex HTML layout are required
  • In enterprise applications with encryption requirements

gomail is a reliable choice for most email sending tasks. Thanks to active community support and regular updates, the library remains relevant even for high-load projects.

Recommendations