-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostmark.go
95 lines (80 loc) · 1.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
package gomailer
import (
"context"
"encoding/base64"
"github.com/keighl/postmark"
"net/http"
"strings"
"sync"
"sync/atomic"
"time"
)
type postmarkClient struct {
pClient *postmark.Client
config *Config
m sync.Mutex
isClosed int32
}
func newPostmark(c *Config) *postmarkClient {
pc := postmark.NewClient(c.ServerToken, c.AccountToken)
pc.HTTPClient = &http.Client{Timeout: 10 * time.Second}
return &postmarkClient{
pClient: pc,
config: c,
}
}
func (p *postmarkClient) Send(msg *Message) error {
emails := make([]postmark.Email, 0)
for _, to := range msg.SendTo {
attchs := make([]postmark.Attachment, 0)
if 0 < len(msg.Attachments) {
for _, att := range msg.Attachments {
a := postmark.Attachment{
Content: base64.StdEncoding.EncodeToString(att.Byte),
Name: att.Filename,
}
attchs = append(attchs, a)
}
}
if "" == msg.From {
msg.From = p.config.FromEmail
}
email := postmark.Email{
From: msg.From,
To: to,
Subject: msg.Title,
TrackOpens: true,
Cc: strings.Join(msg.CC, ","),
Bcc: strings.Join(msg.BCC, ","),
Attachments: attchs,
}
if "" == strings.TrimSpace(msg.ContentType) {
email.HtmlBody = msg.Body
} else {
email.TextBody = msg.Body
}
emails = append(emails, email)
}
_, err := p.pClient.SendEmailBatch(emails)
return err
}
func (p *postmarkClient) SendAsync(msg *Message) error {
return p.Send(msg)
}
func (p *postmarkClient) SendContext(ctx context.Context, msg *Message) error {
// TODO implement ctx & worker pool
return p.Send(msg)
}
func (p *postmarkClient) Close() error {
// TODO for worker pool
if nil == p.pClient {
return nil
}
p.m.Lock()
defer p.m.Unlock()
if !atomic.CompareAndSwapInt32(&p.isClosed, 0, stateClosed) {
return nil
}
p.pClient = nil
return nil
}