Go-Mail

Go Mail

go get gopkg.in/mail.v2

发送函数

package email

import (
	"crypto/tls"
	"fmt"

	gomail "gopkg.in/mail.v2"
)

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

	// Set E-Mail sender
	m.SetHeader("From", "jinwei@xx.com")

	// Set E-Mail receivers
	m.SetHeader("To", "jinweichang@xx.com")

	// Set E-Mail subject
	m.SetHeader("Subject", "Gomail test subject")

	// Set E-Mail body. You can set plain text or html with text/html
	m.SetBody("text/plain", "This is Gomail test body")

	// Settings for SMTP server
	d := gomail.NewDialer("172.xx.xx.xx", 25, "jinwei@xx.com", "")

	// This is only needed when SSL/TLS certificate is not valid on server.
	// In production this should be set to false.
	d.TLSConfig = &tls.Config{InsecureSkipVerify: true}

	// Now send E-Mail
	if err := d.DialAndSend(m); err != nil {
		fmt.Println(err)
		panic(err)
	}

	return
}

测试发送

package email

import "testing"

func TestSendEmail(t *testing.T) {
	SendMail()

}

参考资料:https://www.loginradius.com/blog/async/sending-emails-with-golang/

posted @ 2021-07-22 15:32  JinweiChang  阅读(28)  评论(0编辑  收藏  举报