You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
783 B
44 lines
783 B
3 years ago
|
package mail
|
||
3 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"gopkg.in/gomail.v2"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
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)
|
||
|
|
||
|
//设置发送给多个用户
|
||
|
mailArrTo := strings.Split(o.MailTo, ",")
|
||
|
m.SetHeader("To", mailArrTo...)
|
||
|
|
||
|
//设置邮件主题
|
||
|
m.SetHeader("Subject", o.Subject)
|
||
|
|
||
|
//设置邮件正文
|
||
|
m.SetBody("text/html", o.Body)
|
||
|
|
||
|
d := gomail.NewDialer(o.MailHost, o.MailPort, o.MailUser, o.MailPass)
|
||
|
|
||
|
err := d.DialAndSend(m)
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
}
|
||
|
return err
|
||
|
}
|