41 lines
986 B
Go
41 lines
986 B
Go
![]() |
package lib
|
|||
|
|
|||
|
import (
|
|||
|
"gopkg.in/gomail.v2"
|
|||
|
)
|
|||
|
|
|||
|
type Email struct {
|
|||
|
*EmailOption
|
|||
|
}
|
|||
|
|
|||
|
type EmailOption struct {
|
|||
|
Alias, User, Pass string // Auth:定义邮箱服务器连接信息,如果是阿里邮箱 Pass填密码,qq邮箱填授权码
|
|||
|
Host string
|
|||
|
Port int
|
|||
|
}
|
|||
|
|
|||
|
// EmailSendHandle 邮件发送处理
|
|||
|
// @objects:发送对象,多个邮件地址
|
|||
|
// @subject:邮件主题
|
|||
|
// @body:邮件正文
|
|||
|
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()
|
|||
|
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)
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
func NewEmail(option *EmailOption) *Email {
|
|||
|
return &Email{option}
|
|||
|
}
|