email of Go Language Library Series

Keywords: Go github

Guide reading

Hello, I'm pingye. Today I'd like to introduce a Go language library email for sending emails. At present, the star is 1.3k, which is very easy to use.

Speed up


Initialize project

go get

Project structure of this example

├── go.mod
├── go.sum
└── main.go

To start the SMTP service of e-mail, take 163 e-mail as an example, you need to log in to the background of e-mail and click the start button. After sending the SMS, you will get an authorization code, which will be saved and used later.

Code example

First, configure the mail content

e := email.NewEmail()
e.From = "Ping Ye<>"
e.To = []string{""}
e.Subject = "Find the big secret!"
e.Text = []byte("Ping is so handsome and intelligent~")
  • From: sender's mailbox, in the format of "name + < mailbox >", or you can write directly to the mailbox. The defau lt sender is the name before the @ symbol
  • To: email address of the recipient
  • Subject: Message title
  • Text: message body

Call the Send method to Send mail. The first parameter is the SMTP domain name + port number of your sender's mailbox. The second parameter is used for authentication

e.Send("", smtp.PlainAuth("", "", "<Your password>", ""))


  • Parameter 1: normally, identity should be an empty string to use as the user name.
  • Parameter 2: user name
  • Parameter 3: password. If the authorization code is obtained, fill in the authorization code
  • Parameter 4: server address. The address of 163 is, which can be viewed by other platforms

The complete code is as follows

package main

import (


func main() {
	e := email.NewEmail()
	//e.From = ""
	e.From = "Ping Ye <>"
	e.To = []string{""}
	e.Subject = "Find the big secret!"
	e.Text = []byte("Ping is so handsome and intelligent~")
	err := e.Send("", smtp.PlainAuth("", "", "KQHQCZSXQWPDZYRF", ""))
	if err != nil {


CC and BCC

CC's full name is Carbon Copy, which means CC. BCC's full name is Blind Carbon Copy, which means dark CC. The addressee can't see who is being copied to.

e := email.NewEmail()
e.Cc = []string{""}
e.Bcc = []string{""}

Another initialization method

Direct assignment when instantiation is possible

e := &email.Email{
  From:    "Ping Ye <>",
  To:      []string{""},
  Subject: "Find the big secret!",
  Text:    []byte("Ping is so handsome and intelligent~"),

Sending attachments

It's very simple to send the attachment. Just pass in the filename directly


You can also call Attach to send attachments. AttachFile is actually the encapsulation of Attach

func (e *Email) AttachFile(filename string) (a *Attachment, err error) {
	f, err := os.Open(filename)
	if err != nil {
	defer f.Close()

	ct := mime.TypeByExtension(filepath.Ext(filename))
	basename := filepath.Base(filename)
	return e.Attach(f, basename, ct)

Connection pool

Because frequent sending of e-mail will continuously establish a connection with the SMTP server, which will affect the performance, e-mail provides the function of connection pool

auth := smtp.PlainAuth("", "", "<Your password>", "")
p, _ := email.NewPool("", 4, auth)

After the creation is successful, you can send mail through the connection pool

e := email.NewEmail()
e.From = "Ping Ye <>"
e.To = []string{""}
e.Subject = "Find the big secret!"
e.Text = []byte("Ping is so handsome and intelligent~")

p.Send(e, 10*time.Second)

Go language library code example, welcome star

Thank you for watching. If you think the article is helpful, please pay attention to the official account of "Ping Yi" and focus on the principle of Go language and technology.

Posted by Ayon on Sun, 05 Apr 2020 22:05:15 -0700