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.
43 lines
1001 B
43 lines
1001 B
4 years ago
|
package email
|
||
|
|
||
|
import (
|
||
|
"encoding/base64"
|
||
|
"gopkg.in/gomail.v2"
|
||
|
"path/filepath"
|
||
|
)
|
||
|
|
||
|
var Email = &email{}
|
||
|
|
||
|
type email struct {
|
||
|
dialer *gomail.Dialer
|
||
|
mail string
|
||
|
}
|
||
|
type Attachment struct {
|
||
|
Name string
|
||
|
Path string
|
||
|
}
|
||
|
|
||
|
// InitEmail 初始化配置
|
||
|
func InitEmail(smtp string, port int, user, pass, mail string) *email {
|
||
|
Email.dialer = gomail.NewDialer(smtp, port, user, pass)
|
||
|
Email.mail = mail
|
||
|
return Email
|
||
|
}
|
||
|
|
||
|
// Send @Title 发送邮件
|
||
|
func (e *email) Send(email, subject, body string, attachments []Attachment) error {
|
||
|
message := gomail.NewMessage()
|
||
|
message.SetHeader("From", e.mail)
|
||
|
message.SetHeader("To", email)
|
||
|
message.SetHeader("Subject", subject)
|
||
|
message.SetBody("text/html", body)
|
||
|
for _, attachment := range attachments {
|
||
|
ext := filepath.Ext(attachment.Path)
|
||
|
message.Attach(attachment.Path, gomail.Rename("=?utf-8?B?"+base64.StdEncoding.EncodeToString([]byte(attachment.Name+ext))+"?="))
|
||
|
}
|
||
|
if err := e.dialer.DialAndSend(message); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|