-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation_send_email_template.go
184 lines (162 loc) · 5.07 KB
/
operation_send_email_template.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
package googlemail
import (
"context"
"encoding/base64"
"errors"
"fmt"
"strings"
"google.golang.org/api/gmail/v1"
"google.golang.org/api/option"
"github.com/wakflo/go-sdk/autoform"
sdk "github.com/wakflo/go-sdk/connector"
sdkcore "github.com/wakflo/go-sdk/core"
)
type sendTemplateMailOperationProps struct {
To string `json:"to"`
TemplateSubject string `json:"temp-subject"`
Subject string `json:"subject"`
Body string `json:"body"`
CC string `json:"cc"`
BCC string `json:"bcc"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
FromName string `json:"from"`
}
type SendTemplateMailOperation struct {
options *sdk.OperationInfo
}
func NewSendTemplateMailOperation() sdk.IOperation {
return &SendTemplateMailOperation{
options: &sdk.OperationInfo{
Name: "Send Template Email",
Description: "Send an personalized email.",
RequireAuth: true,
Auth: sharedAuth,
Input: map[string]*sdkcore.AutoFormSchema{
"temp-subject": autoform.NewShortTextField().
SetDisplayName(" Template title").
SetDescription("The template title.").
SetRequired(true).
Build(),
"subject": autoform.NewShortTextField().
SetDisplayName(" Subject").
SetDescription("The template subject you want to send.").
SetRequired(true).
Build(),
"to": autoform.NewShortTextField().
SetDisplayName(" To").
SetDescription("The receiver of the message address").
SetRequired(true).
Build(),
"from": autoform.NewShortTextField().
SetDisplayName(" From").
SetDescription("The sender of the message address").
SetRequired(false).
Build(),
"firstName": autoform.NewShortTextField().
SetDisplayName("First Name").
SetDescription("First name").
SetRequired(true).
Build(),
"lastName": autoform.NewShortTextField().
SetDisplayName("Last Name").
SetDescription("Last name").
SetRequired(false).
Build(),
},
ErrorSettings: sdkcore.StepErrorSettings{
ContinueOnError: false,
RetryOnError: false,
},
},
}
}
func (c *SendTemplateMailOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) {
if ctx.Auth.Token == nil {
return nil, errors.New("missing google auth token")
}
input := sdk.InputToType[sendTemplateMailOperationProps](ctx)
gmailService, err := gmail.NewService(context.Background(), option.WithTokenSource(*ctx.Auth.TokenSource))
if err != nil {
return nil, err
}
userProfile, err := gmailService.Users.GetProfile("me").Do()
if err != nil {
return nil, err
}
userEmail := userProfile.EmailAddress
if !isValidEmail(input.To) {
return nil, fmt.Errorf("invalid To email address: %s", input.To)
}
bccEmails := strings.Split(input.BCC, ",")
for _, email := range bccEmails {
email = strings.TrimSpace(email)
if email != "" && !isValidEmail(email) {
return nil, fmt.Errorf("invalid BCC email address: %s", email)
}
}
ccEmails := strings.Split(input.CC, ",")
for _, email := range ccEmails {
email = strings.TrimSpace(email)
if email != "" && !isValidEmail(email) {
return nil, fmt.Errorf("invalid CC email address: %s", email)
}
}
// Search for the template message
query := "subject:" + input.TemplateSubject
searchResult, err := gmailService.Users.Messages.List("me").Q(query).Do()
if err != nil {
return nil, errors.New("error searching for template")
}
if len(searchResult.Messages) == 0 {
return nil, errors.New("no template found with subject: " + input.TemplateSubject)
}
// Use the first matching message as the template
templateMsg, err := gmailService.Users.Messages.Get("me", searchResult.Messages[0].Id).Do()
if err != nil {
return nil, errors.New("error fetching template message")
}
// Decode the email body
var emailBody string
for _, part := range templateMsg.Payload.Parts {
if part.MimeType == "text/html" {
data, _ := base64.URLEncoding.DecodeString(part.Body.Data)
emailBody = string(data)
break
}
}
if emailBody == "" {
return nil, errors.New("no HTML body found in template message")
}
emailBody = strings.ReplaceAll(emailBody, "{{FirstName}}", input.FirstName)
emailBody = strings.ReplaceAll(emailBody, "{{LastName}}", input.LastName)
var fromField string
if input.FromName != "" {
fromField = fmt.Sprintf("%s <%s>", input.FromName, userEmail)
} else {
fromField = userEmail
}
// Use the modified template as the email body
message := &gmail.Message{
Raw: base64.URLEncoding.EncodeToString([]byte(
"From: " + fromField + "\r\n" +
"To: " + input.To + "\r\n" +
"Subject: " + input.TemplateSubject + "\r\n" +
"Content-Type: text/html; charset=UTF-8\r\n\r\n" +
emailBody)),
}
// Send the message
_, err = gmailService.Users.Messages.Send("me", message).Do()
if err != nil {
return nil, err
}
return map[string]interface{}{
"status": "Message sent successfully!",
}, nil
}
func (c *SendTemplateMailOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) {
return c.Run(ctx)
}
func (c *SendTemplateMailOperation) GetInfo() *sdk.OperationInfo {
return c.options
}