2021-09-28 11:47:19 +08:00
|
|
|
|
package lib
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"gopkg.in/gomail.v2"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Email struct {
|
|
|
|
|
*EmailOption
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type EmailOption struct {
|
2021-10-22 17:10:43 +08:00
|
|
|
|
Alias, User, Pass string // Auth:定义邮箱服务器连接信息,如果是阿里邮箱 Pass填密码,qq邮箱填授权码
|
|
|
|
|
Host string
|
|
|
|
|
Port int
|
2021-09-28 11:47:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-22 17:10:43 +08:00
|
|
|
|
// EmailSendHandle 邮件发送处理
|
|
|
|
|
// @objects:发送对象,多个邮件地址
|
|
|
|
|
// @subject:邮件主题
|
|
|
|
|
// @body:邮件正文
|
2021-09-28 11:47:19 +08:00
|
|
|
|
type EmailSendHandle func(objects []string, subject, body string) error
|
|
|
|
|
|
|
|
|
|
func (this *Email) Init() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *Email) Send() EmailSendHandle {
|
|
|
|
|
return func(objects []string, subject, body string) error {
|
|
|
|
|
m := gomail.NewMessage()
|
2021-10-22 17:10:43 +08:00
|
|
|
|
m.SetHeader("From", m.FormatAddress(this.User, this.Alias))
|
|
|
|
|
m.SetHeader("To", objects...)
|
|
|
|
|
m.SetHeader("Subject", subject)
|
|
|
|
|
m.SetBody("text/html", body)
|
|
|
|
|
return gomail.NewDialer(this.Host, this.Port, this.User, this.Pass).DialAndSend(m)
|
2021-09-28 11:47:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewEmail(option *EmailOption) *Email {
|
|
|
|
|
return &Email{option}
|
|
|
|
|
}
|