commit
49090d766a
@ -0,0 +1,42 @@
|
|||||||
|
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
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
module git.oa00.com/go/email
|
||||||
|
|
||||||
|
go 1.16
|
||||||
|
|
||||||
|
require (
|
||||||
|
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
|
||||||
|
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
|
||||||
|
)
|
@ -0,0 +1,4 @@
|
|||||||
|
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
|
||||||
|
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
|
||||||
|
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
|
||||||
|
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
|
Loading…
Reference in new issue