-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
137 lines (119 loc) · 3.33 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package email
import (
"encoding/base64"
"errors"
"fmt"
"net"
"net/mail"
"net/smtp"
"regexp"
"strings"
"time"
)
var (
emailRegexp = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
)
// ValidateFormat checks whether the email address is valid in format
func ValidateFormat(email string) error {
if !emailRegexp.MatchString(email) {
return errors.New("invalid format")
}
return nil
}
// ValidateReachability checks whether the email address is reachable
func ValidateReachability(email string, timeout time.Duration) error {
i := strings.LastIndexByte(email, '@')
host := email[i+1:]
// The following operation will have a timeout.
deadline := time.After(timeout)
c := make(chan (error))
go func() {
mx, err := net.LookupMX(host)
if err != nil {
c <- errors.New("unresolvable host")
return
}
fmt.Println("host", mx[0].Host, "reachable")
// client, err := smtp.Dial(fmt.Sprintf("%s:%d", mx[0].Host, 25))
// if err != nil {
// fmt.Println("host", mx[0].Host, "dial failed")
// c <- err
// return
// }
// fmt.Println("host", mx[0].Host, "dial done")
// defer client.Close()
// err = client.Hello("checkmail.me")
// if err != nil {
// c <- err
// fmt.Println("host", mx[0].Host, "hello not sent")
// return
// }
// fmt.Println("host", mx[0].Host, "hello sent")
// err = client.Mail("wumuxian1988@gmail.com")
// if err != nil {
// c <- err
// fmt.Println("host", mx[0].Host, "mail command not successful")
// return
// }
// fmt.Println("host", mx[0].Host, "mail command successful")
// err = client.Rcpt(email)
// if err != nil {
// c <- err
// fmt.Println("host", mx[0].Host, "recpt not done")
// return
// }
// fmt.Println("host", mx[0].Host, "recpt done")
c <- nil
}()
select {
case <-deadline:
return errors.New("check reachiability timeout")
case err := <-c:
return err
}
}
// Send send emails
// to - the receiver's email
// subject - the subject
// content - the email content body
// authName - SMTP auth name
// authPass - SMTP auth password
// authAddr - SMTP auth address
// senderName - The sender's name
// senderAddr - The sender's email address
func SendSMTP(to, subject, content, authName, authPass, authAddr, senderName, senderAddr string) (err error) {
err = ValidateFormat(to)
if err != nil {
return
}
err = ValidateReachability(to, time.Second*3)
if err != nil {
return
}
// Set up authentication information.
auth := smtp.PlainAuth("", authName, authPass, authAddr)
header := make(map[string]string)
fromAddr := mail.Address{
Name: senderName,
Address: senderAddr,
}
toAddr := mail.Address{
Name: "",
Address: to,
}
header["From"] = fromAddr.String()
header["To"] = toAddr.String()
header["Subject"] = subject
header["MIME-Version"] = "1.0"
header["Content-Type"] = "text/html; charset=\"utf-8\""
header["Content-Transfer-Encoding"] = "base64"
message := ""
for k, v := range header {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(content))
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
err = smtp.SendMail(authAddr+":25", auth, fromAddr.Address, []string{to}, []byte(message))
return
}