/
email.go
73 lines (64 loc) · 1.62 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package auth
import (
"crypto/tls"
"fmt"
"net/smtp"
"regexp"
"strconv"
"strings"
"gopkg.in/gomail.v2"
)
// email push
var (
v_email_from = ""
v_email_host = ""
v_email_passwd = ""
)
func InitEmail(host, from, pwd string) {
v_email_from = from
v_email_host = host
v_email_passwd = pwd
}
func isValidEmail(email string) bool {
isValid, _ := regexp.MatchString("^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$", email)
return isValid
}
func PushByEmail(pushMail, pushTitle, pushDesc, content string, fCallBack func(error)) (string, error) {
if !isValidEmail(pushMail) {
return "", fmt.Errorf("invalid email format, please input again, email: %v, content: %s", pushMail, content)
}
pushContent := content
if pushDesc != "" {
pushContent = fmt.Sprintf("%v\n%v", pushDesc, content)
}
go func() {
err := sendEmailTLS("", pushMail, pushTitle, pushContent)
if err != nil {
if fCallBack != nil {
fCallBack(err)
}
}
}()
return pushContent, nil
}
func sendEmailTLS(from, to, subject, msg string) error {
if from == "" {
from = v_email_from
}
m := gomail.NewMessage()
m.SetHeader("From", from)
m.SetHeader("To", to)
m.SetHeader("Subject", subject)
m.SetBody("text/plain", msg)
port, _ := strconv.Atoi(strings.Split(v_email_host, ":")[1])
d := &gomail.Dialer{
Port: port,
SSL: true,
Username: v_email_from,
Password: v_email_passwd,
Host: strings.Split(v_email_host, ":")[0],
TLSConfig: &tls.Config{InsecureSkipVerify: true},
Auth: smtp.PlainAuth(v_email_passwd, v_email_from, v_email_passwd, strings.Split(v_email_host, ":")[0]),
}
return d.DialAndSend(m)
}