Email Sending 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

Email Sending in Go (Golang): A Complete Overview of Libraries and Tools



In modern web development, sending emails remains a key task for notifications, registrations, password recovery, and newsletters. The Go language (Golang) offers several effective ways to integrate with SMTP servers. In this article, we will cover the standard library net/smtp, the popular third-party library gomail, and the advanced go-smtp. You will learn how to send simple text and HTML emails, add attachments, and configure a secure connection.



1. Standard Library net/smtp



The net/smtp package is built into Go and requires no additional installation. It provides basic functions for connecting to an SMTP server, authentication, and sending messages. However, working with modern requirements (HTML, attachments, SSL/TLS) will require writing more code.



Simple Example of Sending a Text Email



package main

import ( "fmt" "net/smtp")

func main() { // SMTP server settings (example for Gmail) smtpHost := "smtp.gmail.com" smtpPort := "587" from := "your_email@gmail.com" password := "your_app_password" to := []string{"recipient@example.com"}

// Subject and body of the email (simplest format) subject := "Subject: Test email from Go" body := "Hello! This message was sent using net/smtp." msg := []byte(subject + "" + 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!")}


Important: For Gmail, you need to use an app password (not your main password) and allow access for less secure apps. For other services (Yandex, Mail.ru), the parameters may differ.



Sending an HTML Email via net/smtp



To send an HTML email, you need to explicitly specify the Content-Type header:



package main

import ( "fmt" "net/smtp")

func main() { smtpHost := "smtp.gmail.com" smtpPort := "587" from := "your_email@gmail.com" password := "your_app_password" to := []string{"recipient@example.com"}

// Form MIME headers headers := make(map[string]string) headers["From"] = from headers["To"] = "recipient@example.com" headers["Subject"] = "HTML email from Go" headers["MIME-Version"] = "1.0" headers["Content-Type"] = "text/html; charset=\\"UTF-8\\""

// Build the message message := "" for k, v := range headers { message += fmt.Sprintf("%s: %s\\r", k, v) } message += "\\r" + "<h1>Hello!</h1><p>This is an <b>HTML</b> email.</p>"

auth := smtp.PlainAuth("", from, password, smtpHost) err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, []byte(message)) if err != nil { fmt.Println("Error:", err) }}


The disadvantage of net/smtp is the lack of support for attachments and complex multi-part email structures. For such tasks, it is better to use specialized libraries.



2. The gomail Library



Gomail is one of the most popular third-party libraries for sending email in Go. It provides a convenient API, support for HTML, attachments, image embedding, SSL/TLS, and customizable headers.



Installation



go get gopkg.in/mail.v2


Example of Sending an HTML Email with an Attachment



package main

import ( "crypto/tls" "fmt" gomail "gopkg.in/mail.v2")

func main() { m := gomail.NewMessage(

Recommendations