-
Notifications
You must be signed in to change notification settings - Fork 467
/
postmark.go
169 lines (142 loc) · 4.82 KB
/
postmark.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
/*
Copyright 2021 The Dapr 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 postmark
import (
"context"
"errors"
"fmt"
"reflect"
"strconv"
"github.com/mrz1836/postmark"
"github.com/dapr/components-contrib/bindings"
"github.com/dapr/components-contrib/metadata"
"github.com/dapr/kit/logger"
)
// Postmark allows sending of emails using the 3rd party Postmark service.
type Postmark struct {
metadata postmarkMetadata
logger logger.Logger
}
// Our metadata holds standard email properties.
type postmarkMetadata struct {
ServerToken string `mapstructure:"serverToken"`
AccountToken string `mapstructure:"accountToken"`
EmailFrom string `mapstructure:"emailFrom"`
EmailTo string `mapstructure:"emailTo"`
Subject string `mapstructure:"subject"`
EmailCc string `mapstructure:"emailCc"`
EmailBcc string `mapstructure:"emailBcc"`
}
// NewPostmark returns a new Postmark bindings instance.
func NewPostmark(logger logger.Logger) bindings.OutputBinding {
return &Postmark{logger: logger}
}
// Helper to parse metadata.
func (p *Postmark) parseMetadata(meta bindings.Metadata) (postmarkMetadata, error) {
pMeta := postmarkMetadata{}
err := metadata.DecodeMetadata(meta.Properties, &pMeta)
if err != nil {
return pMeta, err
}
// Required properties
if pMeta.ServerToken == "" {
return pMeta, errors.New("Postmark binding error: serverToken field is required in metadata")
}
if pMeta.AccountToken == "" {
return pMeta, errors.New("Postmark binding error: accountToken field is required in metadata")
}
return pMeta, nil
}
// Init does metadata parsing and not much else :).
func (p *Postmark) Init(_ context.Context, metadata bindings.Metadata) error {
// Parse input metadata
meta, err := p.parseMetadata(metadata)
if err != nil {
return err
}
// Um, yeah that's about it!
p.metadata = meta
return nil
}
// Operations returns list of operations supported by Postmark binding.
func (p *Postmark) Operations() []bindings.OperationKind {
return []bindings.OperationKind{bindings.CreateOperation}
}
// Invoke does the work of sending message to Postmark API.
func (p *Postmark) Invoke(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
// We allow two possible sources of the properties we need,
// the component metadata or request metadata, request takes priority if present
var email postmark.Email
// Build email from address, this is required
if p.metadata.EmailFrom != "" {
email.From = p.metadata.EmailFrom
}
if req.Metadata["emailFrom"] != "" {
email.From = req.Metadata["emailFrom"]
}
if len(email.From) == 0 {
return nil, fmt.Errorf("error Postmark from email not supplied")
}
// Build email to address, this is required
if p.metadata.EmailTo != "" {
email.To = p.metadata.EmailTo
}
if req.Metadata["emailTo"] != "" {
email.To = req.Metadata["emailTo"]
}
if len(email.To) == 0 {
return nil, fmt.Errorf("error Postmark to email not supplied")
}
// Build email subject, this is required
if p.metadata.Subject != "" {
email.Subject = p.metadata.Subject
}
if req.Metadata["subject"] != "" {
email.Subject = req.Metadata["subject"]
}
if len(email.Subject) == 0 {
return nil, fmt.Errorf("error Postmark subject not supplied")
}
// Build email cc address, this is optional
if p.metadata.EmailCc != "" {
email.Cc = p.metadata.EmailCc
}
if req.Metadata["emailCc"] != "" {
email.Cc = req.Metadata["emailCc"]
}
// Build email bcc address, this is optional
if p.metadata.EmailBcc != "" {
email.Bcc = p.metadata.EmailBcc
}
if req.Metadata["emailBcc"] != "" {
email.Bcc = req.Metadata["emailBcc"]
}
// Email body is held in req.Data, after we tidy it up a bit
emailBody, _ := strconv.Unquote(string(req.Data))
email.HTMLBody = emailBody
// Send the email
client := postmark.NewClient(p.metadata.ServerToken, p.metadata.AccountToken)
_, err := client.SendEmail(ctx, email)
if err != nil {
return nil, fmt.Errorf("error from Postmark, sending email failed: %+v", err)
}
p.logger.Info("sent email with Postmark")
return nil, nil
}
// GetComponentMetadata returns the metadata of the component.
func (p *Postmark) GetComponentMetadata() map[string]string {
metadataStruct := postmarkMetadata{}
metadataInfo := map[string]string{}
metadata.GetMetadataInfoFromStructType(reflect.TypeOf(metadataStruct), &metadataInfo, metadata.BindingType)
return metadataInfo
}