-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.go
125 lines (98 loc) Β· 2.35 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
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
package wail
import (
"errors"
"net/mail"
)
type encoding string
const (
QuotedPrintable encoding = "quoted-printable"
Base64 encoding = "base64"
)
type charset string
const (
UTF8 charset = "UTF-8"
ISO_8859_1 charset = "ISO-8859-1"
US_ASCII charset = "US-ASCII"
)
type recipients []string
type MailConfig struct {
Charset charset
Encoding encoding
}
type Mail struct {
cfg *MailConfig
mb *mimeBuilder
recipients recipients
}
var DefaultMailConfig MailConfig = MailConfig{
Charset: UTF8,
Encoding: Base64,
}
func NewMail(cfg *MailConfig) *Mail {
var m *Mail
if cfg != nil {
if cfg.Charset == "" {
cfg.Charset = UTF8
}
if cfg.Encoding == "" {
cfg.Encoding = QuotedPrintable
}
m = &Mail{
cfg: &MailConfig{
Charset: cfg.Charset,
Encoding: cfg.Encoding,
},
}
} else {
m = &Mail{cfg: &DefaultMailConfig}
}
m.mb = newMimeBuilder(m.cfg.Charset, m.cfg.Encoding)
m.recipients = make(recipients, 0, 10)
return m
}
// SetSubject sets an email subject. Subject could be empty
func (m *Mail) SetSubject(subj string) {
m.mb.SetFieldSubject(subj)
}
func (m *Mail) validateAndAppendEmails(emails []string) error {
if len(emails) == 0 {
return errors.New("wail: an empty email address list has been provided")
}
for _, email := range emails {
if len(email) > 254 {
return errors.New("wail: length of the email address must be less than 254 chars")
} else if _, err := mail.ParseAddress(email); err != nil {
return err
}
}
m.recipients = append(m.recipients, emails...)
return nil
}
// To sets main email addresses to which an email will be sent
func (m *Mail) To(emails ...string) error {
if err := m.validateAndAppendEmails(emails); err != nil {
return err
}
m.mb.SetFieldTo(emails...)
return nil
}
// CopyTo sets email addresses to which an email copy will be sent
func (m *Mail) CopyTo(emails ...string) error {
if err := m.validateAndAppendEmails(emails); err != nil {
return err
}
m.mb.SetFieldCc(emails...)
return nil
}
// BlindCopyTo sets email addresses to which an email blind copy will be sent
func (m *Mail) BlindCopyTo(emails ...string) error {
if err := m.validateAndAppendEmails(emails); err != nil {
return err
}
m.mb.SetFieldBcc(emails...)
return nil
}
// SetMessage sets an email message
func (m *Mail) SetMessage(msg Message) {
m.mb.SetMessage(msg)
}