• 周五. 12 月 27th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Go-Mail

admin

11 月 28, 2021

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", "[email protected]")

	// Set E-Mail receivers
	m.SetHeader("To", "[email protected]")

	// 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, "[email protected]", "")

	// 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/

发表回复