-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathemail.go
72 lines (58 loc) · 1.58 KB
/
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
package email
import (
"fmt"
"golang.org/x/oauth2"
"github.com/opensourceways/app-cla-server/util"
)
var EmailAgent = &emailAgent{emailClients: map[string]IEmail{}}
type IEmail interface {
GetOauth2CodeURL(state string) string
GetToken(code, scope string) (*oauth2.Token, error)
GetAuthorizedEmail(token *oauth2.Token) (string, error)
SendEmail(token *oauth2.Token, msg *EmailMessage) error
initialize(string) error
}
func Initialize(configFile string) error {
if err := initTemplate(); err != nil {
return err
}
cfg := emailConfigs{}
if err := util.LoadFromYaml(configFile, &cfg); err != nil {
return err
}
EmailAgent.webRedirectDir = cfg.webRedirectDirConfig
for _, item := range cfg.Configs {
e, err := EmailAgent.GetEmailClient(item.Platform)
if err != nil {
return err
}
if err = e.initialize(item.Credentials); err != nil {
return err
}
}
return nil
}
type EmailMessage struct {
From string `json:"from"`
To []string `json:"to"`
Subject string `json:"subject"`
Content string `json:"content"`
Attachment string `json:"attachment"`
}
type emailAgent struct {
emailClients map[string]IEmail
webRedirectDir webRedirectDirConfig
}
func (this *emailAgent) WebRedirectDir(success bool) string {
if success {
return this.webRedirectDir.WebRedirectDirOnSuccess
}
return this.webRedirectDir.WebRedirectDirOnFailure
}
func (this *emailAgent) GetEmailClient(platform string) (IEmail, error) {
e, ok := this.emailClients[platform]
if !ok {
return nil, fmt.Errorf("it only supports gmail platform currently")
}
return e, nil
}