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.
 
 

82 lines
1.7 KiB

package mail
import (
"crypto/tls"
"fmt"
"mime"
"path/filepath"
"strings"
conf "gitea.baoapi.com/root/stu_uuos/config"
"gopkg.in/gomail.v2"
)
type EMailClient struct {
from string
host string
port int
username string
password string
to []string
subject []string
content []string
attach []string
}
func NewClient() *EMailClient {
return &EMailClient{
from: conf.GetConfig().Mail.Username,
host: conf.GetConfig().Mail.Host,
port: conf.GetConfig().Mail.Port,
username: conf.GetConfig().Mail.Username,
password: conf.GetConfig().Mail.Password,
}
}
func (c *EMailClient) AddTo(to ...string) {
c.to = append(c.to, to...)
}
func (c *EMailClient) AddSubject(subject ...string) {
c.subject = append(c.subject, subject...)
}
func (c *EMailClient) AddContent(content ...string) {
c.content = append(c.content, content...)
}
func (c *EMailClient) AddAttach(attach ...string) {
c.attach = append(c.attach, attach...)
}
func (c *EMailClient) Send() error {
m := gomail.NewMessage()
m.SetHeader("From", c.from)
m.SetHeader("To", c.to...)
m.SetHeader("Subject", c.subject...)
buf := strings.Builder{}
for _, s := range c.content {
buf.WriteString("<p>" + s + "</p>")
}
for _, v := range c.attach {
fileName := filepath.Base(v)
m.Attach(v, gomail.Rename(fileName),
gomail.SetHeader(map[string][]string{
"Content-Disposition": []string{
fmt.Sprintf(`attachment; filename="%s"`, mime.QEncoding.Encode("UTF-8", fileName)),
},
}))
}
m.SetBody("text/html", buf.String())
d := gomail.NewDialer(c.host, c.port, c.username, c.password)
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
return d.DialAndSend(m)
}