-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
171 lines (149 loc) · 3.53 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/smtp"
"os"
"strconv"
"time"
)
var contextChan chan *Context
func main() {
numWorkers := 32
contextChan = make(chan *Context, 10)
CreateWorkers(numWorkers, contextChan)
http.HandleFunc("/send", HandleAllRequest)
http.ListenAndServe(":80", nil)
}
func CreateWorkers(numWorkers int, contextChan chan *Context) {
for i := 0; i < numWorkers; i++ {
go func(contextChan chan *Context) {
for context := range contextChan {
sendHandler(context.RequestBody)
}
}(contextChan)
}
}
func HandleAllRequest(w http.ResponseWriter, r *http.Request) {
jsonBody, _ := io.ReadAll(r.Body)
contextChan <- &Context{Response: w, RequestBody: jsonBody}
}
func sendHandler(jsonBody []byte) {
var emailBody EmailBody
errJson := json.Unmarshal(jsonBody, &emailBody)
if errJson != nil {
fmt.Println("body to json err: ", errJson, jsonBody)
return
}
now := time.Now()
timeString := now.Format("2006-01-02 15:04:05")
host := getEnvStr("HOST", emailBody.Host)
port := getEnvInt("PORT", emailBody.Port)
password := getEnvStr("PASSWORD", emailBody.Password)
fromEmail := getEnvStr("FROM_EMAIL", emailBody.FromEmail)
fromName := getEnvStr("FROM_NAME", emailBody.FromName)
toEmail := emailBody.ToEmail
subject := getEnvStr("SUBJECT", emailBody.Subject)
headers := make(map[string]string)
headers["From"] = fromName + " <" + fromEmail + ">"
headers["To"] = toEmail
headers["Subject"] = subject
headers["Content-Type"] = getEnvStr("CONTENT_TYPE", emailBody.ContentType)
message := ""
for k, v := range headers {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + emailBody.Body
errSend := SendMailWithTLS(
fmt.Sprintf("%s:%d", host, port),
smtp.PlainAuth("", fromEmail, password, host),
fromEmail, []string{toEmail}, []byte(message),
)
if errSend != nil {
fmt.Println(timeString, "send email err ", errSend, subject)
} else {
fmt.Println(timeString, "send mail success ", toEmail, subject)
}
}
func SendMailWithTLS(addr string, auth smtp.Auth, from string, to []string, msg []byte) (err error) {
c, err := Dial(addr)
if err != nil {
fmt.Println("Create smtp client error:", err)
return err
}
defer c.Close()
if auth != nil {
if ok, _ := c.Extension("AUTH"); ok {
if err = c.Auth(auth); err != nil {
fmt.Println("Error during AUTH", err)
return err
}
}
}
if err = c.Mail(from); err != nil {
return err
}
for _, addr := range to {
if err = c.Rcpt(addr); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
_, err = w.Write(msg)
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return c.Quit()
}
func Dial(addr string) (*smtp.Client, error) {
conn, err := tls.Dial("tcp", addr, nil)
if err != nil {
fmt.Println("tls.Dial Error:", err)
return nil, err
}
host, _, _ := net.SplitHostPort(addr)
return smtp.NewClient(conn, host)
}
func getEnvStr(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func getEnvInt(key string, fallback int) int {
if value, ok := os.LookupEnv(key); ok {
ret, err := strconv.Atoi(value)
if err != nil {
return fallback
} else {
return ret
}
}
return fallback
}
type Context struct {
Response http.ResponseWriter
RequestBody []byte
}
type EmailBody struct {
ContentType string
FromEmail string
FromName string
Password string
ToEmail string
Subject string
Body string
Host string
Port int
}