-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.go
84 lines (68 loc) · 1.3 KB
/
mail.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
package postman
import (
"context"
"crypto/tls"
"net/smtp"
"time"
"github.com/boxgo/box/pkg/logger"
"github.com/jordan-wright/email"
)
type (
PostMan struct {
cfg *Config
client *email.Pool
}
Email = email.Email
Attachment = email.Attachment
)
var (
NewEmail = email.NewEmail
)
var (
Default = StdConfig("default").Build()
)
func newPostMan(cfg *Config) *PostMan {
client, err := email.NewPool(
cfg.Address,
4,
smtp.PlainAuth(cfg.Identity, cfg.Username, cfg.Password, cfg.Host),
)
if err != nil {
logger.Panicw("Email.NewPool", "config", cfg, "err", err)
}
return &PostMan{
cfg: cfg,
client: client,
}
}
func (pm PostMan) Serve(ctx context.Context) error {
pm.client.SetHelloHostname("")
return nil
}
func (pm PostMan) Shutdown(ctx context.Context) error {
pm.client.Close()
return nil
}
func (pm PostMan) Send(e *Email, t ...time.Duration) error {
if e == nil {
return nil
}
timeout := pm.cfg.Timeout
if len(t) != 0 {
timeout = t[0]
}
if e.From == "" {
e.From = pm.cfg.From
}
if e.From == "" {
e.From = pm.cfg.Username
}
if pm.cfg.SSL {
return e.SendWithTLS(
pm.cfg.Address,
smtp.PlainAuth(pm.cfg.Identity, pm.cfg.Username, pm.cfg.Password, pm.cfg.Host),
&tls.Config{ServerName: pm.cfg.Host},
)
}
return pm.client.Send(e, timeout)
}