37 lines
669 B
Go
37 lines
669 B
Go
|
package mail
|
||
|
|
||
|
import (
|
||
|
"gopkg.in/gomail.v2"
|
||
|
)
|
||
|
|
||
|
type Options struct {
|
||
|
MailHost string
|
||
|
MailPort int
|
||
|
MailUser string // 发件人
|
||
|
MailPass string // 发件人密码
|
||
|
MailTo []string // 多个收件人
|
||
|
Subject string // 邮件主题
|
||
|
Body string // 邮件内容
|
||
|
}
|
||
|
|
||
|
func Send(o *Options) error {
|
||
|
|
||
|
m := gomail.NewMessage()
|
||
|
|
||
|
//设置发件人
|
||
|
m.SetHeader("From", o.MailUser)
|
||
|
|
||
|
//设置发送给多个用户
|
||
|
m.SetHeader("To", o.MailTo...)
|
||
|
|
||
|
//设置邮件主题
|
||
|
m.SetHeader("Subject", o.Subject)
|
||
|
|
||
|
//设置邮件正文
|
||
|
m.SetBody("text/html", o.Body)
|
||
|
|
||
|
d := gomail.NewDialer(o.MailHost, o.MailPort, o.MailUser, o.MailPass)
|
||
|
|
||
|
return d.DialAndSend(m)
|
||
|
}
|