-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemail.go
64 lines (50 loc) · 1.27 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
package sparkle
import "fmt"
type EmailSender interface {
Send(to, from, subject, content string, options ...EmailOption) error
}
type EmailOptions struct {
CC []string
BCC []string
}
type EmailOption func(options *EmailOptions)
func WithCCOption(CC ...string) EmailOption {
return func(options *EmailOptions) {
options.CC = CC
}
}
func WithBCCOption(BCC ...string) EmailOption {
return func(options *EmailOptions) {
options.BCC = BCC
}
}
func ComposeEmailOptions(options ...EmailOption) *EmailOptions {
var opts = EmailOptions{
BCC: []string{},
CC: []string{},
}
for _, option := range options {
option(&opts)
}
return &opts
}
type ConsoleEmailSender struct {
Inbox [][]string
}
func (c *ConsoleEmailSender) Send(to, from, subject, content string, options ...EmailOption) error {
option := ComposeEmailOptions(options...)
message := []string{to, from, subject, content}
for _, bcc := range option.BCC {
message = append(message, bcc)
}
for _, cc := range option.CC {
message = append(message, cc)
}
c.Inbox = append(c.Inbox, message)
fmt.Printf("email sent!!\nfrom:%s -> to:%s", from, to)
fmt.Printf("subject:%s\n", subject)
fmt.Printf("content:%s\n", content)
fmt.Printf("BCC:%v\n", option.BCC)
fmt.Printf("CC:%v\n", option.CC)
return nil
}