37 lines
1.1 KiB
Go
37 lines
1.1 KiB
Go
package lib
|
||
|
||
import (
|
||
"gopkg.in/gomail.v2"
|
||
)
|
||
|
||
type Email struct {
|
||
*EmailOption
|
||
}
|
||
|
||
type EmailOption struct {
|
||
User, Auth string // Auth:定义邮箱服务器连接信息,如果是阿里邮箱 Auth填密码,qq邮箱填授权码
|
||
Host string
|
||
Port int
|
||
}
|
||
|
||
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", "XD Game"+"<"+this.User+">") //这种方式可以添加别名,即“XD Game”, 也可以直接用<code>m.SetHeader("From",mailConn["user"])</code> 读者可以自行实验下效果
|
||
m.SetHeader("To", objects...) //发送给多个用户
|
||
m.SetHeader("Subject", subject) //设置邮件主题
|
||
m.SetBody("text/html", body) //设置邮件正文
|
||
return gomail.NewDialer(this.Host, this.Port, this.User, this.Auth).DialAndSend(m)
|
||
}
|
||
}
|
||
|
||
func NewEmail(option *EmailOption) *Email {
|
||
return &Email{option}
|
||
}
|