-
Notifications
You must be signed in to change notification settings - Fork 872
/
send.go
136 lines (116 loc) · 3.14 KB
/
send.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
/*
Copyright 2021 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package email
import (
"fmt"
"sync"
"gopkg.in/gomail.v2"
"github.com/oam-dev/kubevela/pkg/cue/model/value"
wfContext "github.com/oam-dev/kubevela/pkg/workflow/context"
"github.com/oam-dev/kubevela/pkg/workflow/providers"
"github.com/oam-dev/kubevela/pkg/workflow/types"
)
const (
// ProviderName is provider name for install.
ProviderName = "email"
)
type provider struct {
}
type sender struct {
Address string `json:"address"`
Alias string `json:"alias,omitempty"`
Password string `json:"password"`
Host string `json:"host"`
Port int `json:"port"`
}
type content struct {
Subject string `json:"subject"`
Body string `json:"body"`
}
var emailRoutine sync.Map
// Send sends email
func (h *provider) Send(ctx wfContext.Context, v *value.Value, act types.Action) error {
stepID, err := v.LookupValue("stepID")
if err != nil {
return err
}
id, err := stepID.String()
if err != nil {
return err
}
routine, ok := emailRoutine.Load(id)
if ok {
switch routine {
case "success":
emailRoutine.Delete(id)
return nil
case "initializing", "sending":
act.Wait("wait for the email")
return nil
default:
emailRoutine.Delete(id)
return fmt.Errorf("failed to send email: %v", routine)
}
} else {
emailRoutine.Store(id, "initializing")
}
s, err := v.LookupValue("from")
if err != nil {
return err
}
senderValue := &sender{}
if err := s.UnmarshalTo(senderValue); err != nil {
return err
}
r, err := v.LookupValue("to")
if err != nil {
return err
}
receiverValue := &[]string{}
if err := r.UnmarshalTo(receiverValue); err != nil {
return err
}
c, err := v.LookupValue("content")
if err != nil {
return err
}
contentValue := &content{}
if err := c.UnmarshalTo(contentValue); err != nil {
return err
}
m := gomail.NewMessage()
m.SetAddressHeader("From", senderValue.Address, senderValue.Alias)
m.SetHeader("To", *receiverValue...)
m.SetHeader("Subject", contentValue.Subject)
m.SetBody("text/html", contentValue.Body)
dial := gomail.NewDialer(senderValue.Host, senderValue.Port, senderValue.Address, senderValue.Password)
go func() {
if routine, ok := emailRoutine.Load(id); ok && routine == "initializing" {
emailRoutine.Store(id, "sending")
if err := dial.DialAndSend(m); err != nil {
emailRoutine.Store(id, err.Error())
return
}
emailRoutine.Store(id, "success")
}
}()
act.Wait("wait for the email")
return nil
}
// Install register handlers to provider discover.
func Install(p providers.Providers) {
prd := &provider{}
p.Register(ProviderName, map[string]providers.Handler{
"send": prd.Send,
})
}