This repository has been archived by the owner on Dec 8, 2020. It is now read-only.
forked from shenghui0779/yiigo
/
mailer.go
141 lines (110 loc) · 2.66 KB
/
mailer.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
package yiigo
import (
"fmt"
"sync"
"gopkg.in/gomail.v2"
)
// EMail email
type EMail struct {
Title string
Subject string
From string
To []string
Cc []string
Content string
Attach []string
}
// emailOptions email options
type emailOptions struct {
charset string
encoding gomail.Encoding
contentType string
}
// EMailOption configures how we set up the email
type EMailOption interface {
apply(*emailOptions)
}
// funcEMailOption implements email option
type funcEMailOption struct {
f func(*emailOptions)
}
func (fo *funcEMailOption) apply(o *emailOptions) {
fo.f(o)
}
func newFuncEMailOption(f func(*emailOptions)) *funcEMailOption {
return &funcEMailOption{f: f}
}
// WithEMailCharset specifies the `Charset` to email.
func WithEMailCharset(s string) EMailOption {
return newFuncEMailOption(func(o *emailOptions) {
o.charset = s
})
}
// WithEMailEncoding specifies the `Encoding` to email.
func WithEMailEncoding(e gomail.Encoding) EMailOption {
return newFuncEMailOption(func(o *emailOptions) {
o.encoding = e
})
}
// WithEMailContentType specifies the `ContentType` to email.
func WithEMailContentType(s string) EMailOption {
return newFuncEMailOption(func(o *emailOptions) {
o.contentType = s
})
}
// EMailDialer email dialer
type EMailDialer struct {
dialer *gomail.Dialer
}
// Send send an email.
func (m *EMailDialer) Send(e *EMail, options ...EMailOption) error {
o := &emailOptions{contentType: "text/html"}
if len(options) > 0 {
for _, option := range options {
option.apply(o)
}
}
settings := make([]gomail.MessageSetting, 0, 2)
if o.charset != "" {
settings = append(settings, gomail.SetCharset(o.charset))
}
if o.encoding != "" {
settings = append(settings, gomail.SetEncoding(o.encoding))
}
msg := gomail.NewMessage(settings...)
msg.SetHeader("Subject", e.Subject)
msg.SetAddressHeader("From", e.From, e.Title)
msg.SetHeader("To", e.To...)
if len(e.Cc) > 0 {
msg.SetHeader("Cc", e.Cc...)
}
if len(e.Attach) > 0 {
for _, v := range e.Attach {
msg.Attach(v)
}
}
msg.SetBody(o.contentType, e.Content)
// Send the email
err := m.dialer.DialAndSend(msg)
return err
}
var (
Mailer *EMailDialer
mailerMap sync.Map
)
// RegisterMailer register a mailer
func RegisterMailer(name, host string, port int, account, password string) {
m := &EMailDialer{dialer: gomail.NewDialer(host, port, account, password)}
mailerMap.Store(name, m)
if name == AsDefault {
Mailer = m
}
}
// UseMailer returns a mailer
func UseMailer(name string) *EMailDialer {
v, ok := mailerMap.Load(name)
if !ok {
panic(fmt.Errorf("yiigo: mailer.%s is not registered", name))
}
return v.(*EMailDialer)
}