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/