golang 发送电子邮件
在golang中发送电子邮件有两种方式:
- 使用
net/smtp
- 使用
gopkg.in/gomail.v2
-
使用
net/smtp
package model import ( "fmt" "net/smtp" "strings" ) func SendToMail(userName, password, mailServer, to, subject, body, mailType string) error { // 拼接消息体 var contentType string if mailType == "html" { contentType = "Content-Type: text/" + mailType + "; charset=UTF-8" } else { contentType = "Content-Type: text/plain" + "; charset=UTF-8" } msg := []byte("To: " + to + "\r\nFrom: " + userName + "\r\nSubject: " + subject + "\r\n" + contentType + "\r\n\r\n" + body) // msg 内容输出查看 fmt.Println("To: " + to + "\r\n" + "From: " + userName + "\r\n" + "Subject: " + subject + "\r\n" + "" + contentType + "\r\n\r\n" + "" + body) // 进行身份认证 hp := strings.Split(mailServer, ":") auth := smtp.PlainAuth("", userName, password, hp[0]) sendTo := strings.Split(to, ";") err := smtp.SendMail(mailServer, auth, userName, sendTo, msg) return err } func main() { var userName = "***@163.com" var password = "***" var mailServer = "smtp.163.com:25" var to = "**@qq.com" var subject = "犀点意象安全团队" body := ` 您的服务存在异常,请查收!!! ` bodyHtml :=`<html> <body> <h1>您的服务存在异常</h1> </body> </html> ` fmt.Println("send email") var err error err = SendToMail(userName, password, mailServer, to, subject, body, "") if err != nil { fmt.Println("Send text-mail error!") fmt.Println(err) } else { fmt.Println("Send text-mail success!") } err = SendToMail(userName, password, mailServer, to, subject, bodyHtml, "html") if err != nil { fmt.Println("Send html-mail error!") fmt.Println(err) } else { fmt.Println("Send html-mail success!") } }
-
使用
gopkg.in/gomail.v2
package model import ( "fmt" "gopkg.in/gomail.v2" "net/smtp" "strings" ) const ( // 邮件服务器地址 MailHost = "smtp.163.com" // 端口 MailPort = 25 // 发送邮件用户账号 MailUser = "******@163.com" // 授权密码 MailPwd = "********" ) /* title 使用gomail发送邮件 @param []string mailAddress 收件人邮箱 @param string subject 邮件主题 @param string body 邮件内容 @return error */ func SendGoMail(mailAddress []string, subject string, body string) error { m := gomail.NewMessage() // 这种方式可以添加别名,即 nickname, 也可以直接用<code>m.SetHeader("From", MAIL_USER)</code> nickname := "gomail" m.SetHeader("From",nickname + "<" +MailUser+ ">") // 发送给多个用户 m.SetHeader("To", mailAddress...) // 设置邮件主题 m.SetHeader("Subject", subject) // 设置邮件正文 m.SetBody("text/html", body) d := gomail.NewDialer(MailHost, MailPort, MailUser, MailPwd) // 发送邮件 err := d.DialAndSend(m) return err }
package model import "testing" func TestSendGoMail(t *testing.T) { type args struct { mailAddress []string subject string body string } tests := []struct { name string args args wantErr bool }{ // TODO: Add test cases. { name: "01", args: args{ subject:"您好,这是gomail-text测试邮件", mailAddress:[]string{"******@qq.com"}, body:` 您的服务存在异常,请查收!!! `} , wantErr:false }, { name: "02", args: args{ subject:"您好,这是gomail-html测试邮件", mailAddress:[]string{"*****@qq.com"}, body:`<html> <body> <h1>您的服务存在异常</h1> </body> </html> `} , wantErr:false }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := SendGoMail(tt.args.mailAddress, tt.args.subject, tt.args.body); (err != nil) != tt.wantErr { t.Errorf("SendGoMail() error = %v, wantErr %v", err, tt.wantErr) } }) } }