-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
email.go
38 lines (29 loc) · 1.04 KB
/
email.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package tools
import (
"fmt"
"github.com/eryajf/go-ldap-admin/config"
"strconv"
"gopkg.in/gomail.v2"
)
func email(mailTo []string, subject string, body string) error {
mailConn := map[string]string{
"user": config.Conf.Email.User,
"pass": config.Conf.Email.Pass,
"host": config.Conf.Email.Host,
"port": config.Conf.Email.Port,
}
port, _ := strconv.Atoi(mailConn["port"]) //转换端口类型为int
newmail := gomail.NewMessage()
newmail.SetHeader("From", newmail.FormatAddress(mailConn["user"], config.Conf.Email.From))
newmail.SetHeader("To", mailTo...) //发送给多个用户
newmail.SetHeader("Subject", subject) //设置邮件主题
newmail.SetBody("text/html", body) //设置邮件正文
do := gomail.NewDialer(mailConn["host"], port, mailConn["user"], mailConn["pass"])
return do.DialAndSend(newmail)
}
func SendMail(sendto []string, pass string) error {
subject := "重置LDAP密码成功"
// 邮件正文
body := fmt.Sprintf("<li><a>更改之后的密码为:%s</a></li>", pass)
return email(sendto, subject, body)
}