-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
218 lines (186 loc) · 4.97 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"encoding/base64"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"gopkg.in/gomail.v2"
)
var (
user string
pass string
host string
port string
from string
relayPort string
)
func init() {
// loads values from .env into the system
if err := godotenv.Load(); err == nil {
userName, exists := os.LookupEnv("RELAY_SMTP_USER")
if !exists {
log.Fatalln("Environment variable should contain RELAY_SMTP_USER")
}
user = userName
passUser, exists := os.LookupEnv("RELAY_SMTP_PASS")
if !exists {
log.Fatalln("Environment variable should contain RELAY_SMTP_PASS")
}
pass = passUser
hostUser, exists := os.LookupEnv("RELAY_SMTP_HOST")
if !exists {
log.Fatalln("Environment variable should contain RELAY_SMTP_HOST")
}
host = hostUser
portUser, exists := os.LookupEnv("RELAY_SMTP_PORT")
if !exists {
log.Fatalln("Environment variable should contain RELAY_SMTP_PORT")
}
port = portUser
fromUser, exists := os.LookupEnv("RELAY_FROM_EMAIL")
if !exists {
log.Fatalln("Environment variable should contain RELAY_FROM_EMAIL")
}
from = fromUser
portService, exists := os.LookupEnv("RELAY_SERVICE_PORT")
if !exists {
log.Fatalln("Environment variable should contain RELAY_SERVICE_PORT")
}
relayPort = portService
}
}
func main() {
r := gin.Default()
r.GET("/", hello)
r.GET("/health", health)
r.POST("/api.sendgrid.com/v3/mail/send", sendgrid)
r.POST("/api.sendinblue.com/v3/smtp/email", sendinblue)
r.Run(":" + relayPort)
}
func hello(c *gin.Context) {
c.JSON(200, gin.H{"status": "Hallo"})
}
func health(c *gin.Context) {
c.JSON(200, gin.H{"status": "OK"})
}
func sendgrid(c *gin.Context) {
var body Sendgrid
if c.Request.Header["Authorization"] == nil {
c.JSON(http.StatusBadRequest, gin.H{})
}
token := strings.Split(c.Request.Header["Authorization"][0], " ")[1]
log.Println(token)
if err := c.ShouldBindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
to := mapPersonToEmail(body.Personalizations[0].To)
cc := []string{}
sendMailv2(to, cc, body.Personalizations[0].Subject, body.Content[0].Value, body.Attanctment)
c.JSON(200, gin.H{"status": "OK"})
}
func sendinblue(c *gin.Context) {
var body Sendinblue
if err := c.ShouldBindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
to := mapPersonToEmail(body.To)
cc := []string{}
sendMail(to, cc, body.Subject, body.HTMLContent)
c.JSON(http.StatusAccepted, gin.H{})
}
func sendMailv2(to []string, cc []string, subject, message string, base []ChildAttactment) {
to = []string{to[0]}
m := gomail.NewMessage()
m.SetHeader("From", from)
m.SetHeader("To", to[0])
m.SetHeader("Subject", subject)
m.SetBody("text/html", message)
if len(base) > 0 {
dec, err1 := base64.StdEncoding.DecodeString(base[0].Content)
if err1 != nil {
panic(err1)
}
f, err1 := os.Create(fmt.Sprintf("%s.%s", base[0].Filename, base[0].Type))
if err1 != nil {
panic(err1)
}
defer f.Close()
if _, err := f.Write(dec); err != nil {
panic(err)
}
if err := f.Sync(); err != nil {
panic(err)
}
m.Attach(fmt.Sprintf("./%s.%s", base[0].Filename, base[0].Type))
}
port, _ := strconv.Atoi(port)
d := gomail.NewDialer(host, port, user, pass)
if err := d.DialAndSend(m); err != nil {
log.Println(err)
}
if len(base) > 0 {
e := os.Remove(fmt.Sprintf("./%s.%s", base[0].Filename, base[0].Type))
if e != nil {
log.Fatal(e)
}
}
}
func sendMail(to []string, cc []string, subject, message string) error {
m := gomail.NewMessage()
m.SetHeader("From", from)
m.SetHeader("To", to[0])
m.SetHeader("Subject", subject)
m.SetBody("text/html", message)
port, _ := strconv.Atoi(port)
d := gomail.NewDialer(host, port, user, pass)
err := d.DialAndSend(m)
return err
}
func mapPersonToEmail(person []Person) []string {
res := make([]string, len(person))
for i, val := range person {
res[i] = val.Email
}
return res
}
// Person name and email
type Person struct {
Name string `json:"name"`
Email string `json:"email"`
}
// Sendinblue request body
type Sendinblue struct {
To []Person `json:"to"`
Subject string `json:"subject"`
HTMLContent string `json:"htmlContent"`
}
// Sendgrid request body
type Sendgrid struct {
Personalizations []Personalizations `json:"personalizations"`
From Person `json:"from"`
Subject string `json:"subject"`
Content []SendgridContent `json:"content"`
Attanctment []ChildAttactment `json:"attachments"`
}
// Personalizations for Sendgrid
type Personalizations struct {
To []Person `json:"to"`
Subject string `json:"subject"`
}
// SendgridContent for email body
type SendgridContent struct {
Type string `json:"type"`
Value string `json:"value"`
}
type ChildAttactment struct {
Content string `json:"content"`
Type string `json:"type"`
Filename string `json:"filename"`
}