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

Preparation

Initialize project

go get github.com/jordan-wright/email

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<pingyeaa@163.com>"
e.To = []string{"602588122@qq.com"}
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.163.com:25", smtp.PlainAuth("", "pingyeaa@163.com", "<Your password>", "smtp.163.com"))

smtp.PlainAuth

  • 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 smtp.163.com, which can be viewed by other platforms

The complete code is as follows

package main

import (
	"net/smtp"

	"github.com/jordan-wright/email"
)

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

Expand

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{"xxxxxxx@qq.com"}
e.Bcc = []string{"xxxxxxx@qq.com"}

Another initialization method

Direct assignment when instantiation is possible

e := &email.Email{
  From:    "Ping Ye <pingyeaa@163.com>",
  To:      []string{"xxxxxxx@qq.com"},
  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

e.AttachFile("attachment.txt")

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 {
		return
	}
	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("", "pingyeaa@163.com", "<Your password>", "smtp.163.com")
p, _ := email.NewPool("smtp.163.com:25", 4, auth)

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

e := email.NewEmail()
e.From = "Ping Ye <pingyeaa@163.com>"
e.To = []string{"xxxxxx@qq.com"}
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
https://github.com/pingyeaa/golang-examples

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