-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.go
58 lines (52 loc) · 1.44 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"crypto/tls"
"fmt"
"github.com/jordan-wright/email"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"net/smtp"
)
var (
em *email.Email
)
// 这里暂时不会出现并发的情况
func getEmail() *email.Email {
if em != nil {
return em
}
return email.NewEmail()
}
// 自己发信给自己
func SendEmail(msg string) (err error) {
if GetConf().EmailHost == "" || GetConf().EmailPort == 0 || GetConf().EmailAuthCode == "" {
logrus.Warnf("邮件配置为空,暂不会通过邮件通知。")
return nil
}
e := getEmail()
//设置发送方的邮箱
e.From = GetConf().Email
// 设置接收方的邮箱
e.To = []string{GetConf().Email}
//设置主题
e.Subject = "豆豆豆奶自动签到"
//设置文件发送的内容
e.Text = []byte(msg)
//设置服务器相关的配置
if GetConf().EmailTLS {
err = e.SendWithTLS(fmt.Sprintf("%s:%d", GetConf().EmailHost, GetConf().EmailPort),
smtp.PlainAuth("", GetConf().Email, GetConf().EmailAuthCode, GetConf().EmailHost), &tls.Config{
InsecureSkipVerify: true,
ServerName: GetConf().EmailHost,
})
} else {
err = e.Send(fmt.Sprintf("%s:%d", GetConf().EmailHost, GetConf().EmailPort),
smtp.PlainAuth("", GetConf().Email, GetConf().EmailAuthCode, GetConf().EmailHost))
}
if err != nil {
err = errors.Wrapf(err, "豆豆豆奶自动签到程序发送邮件失败: %s", err.Error())
logrus.Error(err.Error())
return err
}
return nil
}