-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsendgrid-driver.go
More file actions
190 lines (173 loc) · 4.74 KB
/
sendgrid-driver.go
File metadata and controls
190 lines (173 loc) · 4.74 KB
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
// Copyright 2023 Harran Ali <harran.m@gmail.com>. All rights reserved.
// Use of this source code is governed by MIT-style
// license that can be found in the LICENSE file.
package mailing
import (
"encoding/base64"
"errors"
"fmt"
"net/http"
"net/mail"
"os"
"github.com/sendgrid/sendgrid-go"
sgmail "github.com/sendgrid/sendgrid-go/helpers/mail"
)
type SendGridConfig struct {
Host string // "https://api.sendgrid.com"
Endpoint string // "/v3/mail/send"
ApiKey string // SENDGRID_API_KEY
}
type SendGridDriver struct {
config *SendGridConfig
messageBuilder *messageBuilder
from mail.Address
toList []mail.Address
ccList []mail.Address
bccList []mail.Address
subject string
htmlBody string
plainTextBody string
attachments []Attachment
initiateSend func(from string, rcpts []string, message []byte, conf Driver) error
}
var initiateSendGridSend = func(from string, rcpts []string, message []byte, d Driver) error {
sgDriver := d.(*SendGridDriver)
m := sgmail.NewV3Mail()
fromEmail := sgmail.NewEmail(sgDriver.from.Name, sgDriver.from.Address)
m.SetFrom(fromEmail)
m.Subject = sgDriver.subject
p := sgmail.NewPersonalization()
if len(sgDriver.toList) != 0 {
var tos []*sgmail.Email
for _, v := range sgDriver.toList {
tos = append(tos, sgmail.NewEmail(v.Name, v.Address))
}
p.AddTos(tos...)
}
if len(sgDriver.ccList) != 0 {
var ccs []*sgmail.Email
for _, v := range sgDriver.ccList {
ccs = append(ccs, sgmail.NewEmail(v.Name, v.Address))
}
p.AddCCs(ccs...)
}
m.AddPersonalizations(p)
if sgDriver.plainTextBody != "" {
c := sgmail.NewContent("text/plain", sgDriver.plainTextBody)
m.AddContent(c)
}
if sgDriver.htmlBody != "" {
c := sgmail.NewContent("text/html", sgDriver.htmlBody)
m.AddContent(c)
}
var a *sgmail.Attachment
var attachementContent []byte
var err error
for _, v := range sgDriver.attachments {
attachementContent, err = os.ReadFile(v.Path)
if err != nil {
return err
}
encodedAttachmentbuf := base64.StdEncoding.EncodeToString(attachementContent)
fmt.Println(encodedAttachmentbuf)
a = sgmail.NewAttachment()
a.SetContent(encodedAttachmentbuf)
a.SetType(http.DetectContentType(attachementContent))
a.SetFilename(v.Name)
a.SetDisposition("attachment")
m.AddAttachment(a)
}
requestBody := sgmail.GetRequestBody(m)
request := sendgrid.GetRequest(sgDriver.config.ApiKey, sgDriver.config.Endpoint, sgDriver.config.Host)
request.Method = "POST"
var Body = requestBody
request.Body = Body
_, err = sendgrid.API(request)
if err != nil {
return err
}
return nil
}
func initiateSendGrid(config *SendGridConfig) *SendGridDriver {
s := &SendGridDriver{
config: config,
messageBuilder: newMessageBuilder(),
htmlBody: "",
plainTextBody: "",
initiateSend: initiateSendGridSend,
}
return s
}
func (s *SendGridDriver) SetFrom(from mail.Address) error {
s.from = from
return nil
}
func (s *SendGridDriver) SetTo(toList []mail.Address) error {
s.toList = toList
return nil
}
func (s *SendGridDriver) SetCC(ccList []mail.Address) error {
s.ccList = ccList
return nil
}
func (s *SendGridDriver) SetBCC(bccList []mail.Address) error {
s.bccList = bccList
return nil
}
func (s *SendGridDriver) SetSubject(Subject string) error {
s.subject = Subject
return nil
}
func (s *SendGridDriver) SetHTMLBody(body string) error {
s.htmlBody = body
return nil
}
func (s *SendGridDriver) SetPlainTextBody(body string) error {
s.plainTextBody = body
return nil
}
func (s *SendGridDriver) SetAttachments(attachments []Attachment) error {
s.attachments = attachments
return nil
}
func (s *SendGridDriver) Send() error {
// prepare the message
s.messageBuilder.setSubject(s.subject)
if s.htmlBody != "" {
s.messageBuilder.setHTMLBody(s.htmlBody)
} else {
s.messageBuilder.setPlainTextBody(s.plainTextBody)
}
s.messageBuilder.setFrom(s.from)
s.messageBuilder.setToList(s.toList)
s.messageBuilder.setCCList(s.ccList)
s.messageBuilder.setAttachments(s.attachments)
message := s.messageBuilder.build()
// "to" and "cc" message sending
var rcpts []string
for _, v := range s.toList {
rcpts = append(rcpts, v.String())
}
for _, v := range s.ccList {
rcpts = append(rcpts, v.String())
}
from := s.from.String()
err := s.initiateSend(from, rcpts, message, s)
if err != nil {
return errors.New(fmt.Sprintf("error calling s.initiateSend(): %v", err.Error()))
}
// send to bcc
for _, v := range s.bccList {
err = s.initiateSend(from, []string{v.String()}, message, s)
if err != nil {
return errors.New(fmt.Sprintf("error calling s.initiateSend(): %v", err.Error()))
}
}
s.resetDriverProps()
return nil
}
func (s *SendGridDriver) resetDriverProps() {
s.subject = ""
s.htmlBody = ""
s.plainTextBody = ""
}