-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation_send_email.go
134 lines (116 loc) · 3.39 KB
/
operation_send_email.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
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 sendMailOperationProps struct {
To string `json:"to"`
Subject string `json:"subject"`
Body string `json:"body"`
CC string `json:"cc"`
BCC string `json:"bcc"`
}
type SendMailOperation struct {
options *sdk.OperationInfo
}
func NewSendMailOperation() sdk.IOperation {
return &SendMailOperation{
options: &sdk.OperationInfo{
Name: "Send Email",
Description: "Send an email from your Gmail account",
RequireAuth: true,
Auth: sharedAuth,
Input: map[string]*sdkcore.AutoFormSchema{
"subject": autoform.NewShortTextField().
SetDisplayName(" Subject").
SetDescription("The subject of the message").
SetRequired(true).
Build(),
"to": autoform.NewShortTextField().
SetDisplayName(" To").
SetDescription("The receiver of the message address").
SetRequired(true).
Build(),
"body": autoform.NewLongTextField().
SetDisplayName("Body").
SetDescription("Email body").
SetRequired(true).
Build(),
"bcc": autoform.NewShortTextField().
SetDisplayName("BCC").
SetDescription("Blind carbon copy recipients (comma-separated)").
SetRequired(false).
Build(),
"cc": autoform.NewLongTextField().
SetDisplayName("CC").
SetDescription("Carbon copy recipients (comma-separated)").
SetRequired(false).
Build(),
},
ErrorSettings: sdkcore.StepErrorSettings{
ContinueOnError: false,
RetryOnError: false,
},
},
}
}
func (c *SendMailOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) {
if ctx.Auth.Token == nil {
return nil, errors.New("missing google auth token")
}
input := sdk.InputToType[sendMailOperationProps](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
}
fromEmail := 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)
}
}
message := &gmail.Message{
Raw: base64.URLEncoding.EncodeToString([]byte(
"From: " + fromEmail + "\r\n" +
"To: " + input.To + "\r\n" +
"Subject: " + input.Subject + "\r\n" +
"Bcc: " + strings.Join(bccEmails, ",") + "\r\n" +
"Cc: " + strings.Join(ccEmails, ",") + "\r\n\r\n" +
input.Body)),
}
_, err = gmailService.Users.Messages.Send("me", message).Do()
if err != nil {
return nil, err
}
return "Message sent successfully!", nil
}
func (c *SendMailOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) {
return c.Run(ctx)
}
func (c *SendMailOperation) GetInfo() *sdk.OperationInfo {
return c.options
}