-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadress.go
175 lines (148 loc) · 5.02 KB
/
adress.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package MIMEMail
import (
"net/mail"
"net/textproto"
)
// AddressHeader is a dedicated type for mail AddressHeader fields.
type AddressHeader string
// Valid values for mail address headers
const (
AddrSender AddressHeader = "Sender"
AddrFrom AddressHeader = "From"
AddrTo AddressHeader = "To"
AddrCc AddressHeader = "Cc"
AddrBcc AddressHeader = "Bcc"
AddrReplyTo AddressHeader = "ReplyTo"
AddrFollowupTo AddressHeader = "FollowupTo"
)
// Addresses handles setting and encoding the mail address headers
type Addresses map[AddressHeader][]mail.Address
// NewAddresses creates a new mail address header
func NewAddresses() Addresses {
return make(Addresses, 3)
}
// Recipients returns just the mailaddresses of all the recipients
// (To, Cc, Bcc), ready to be passed to smtp.SendMail et al.
func (a Addresses) Recipients() []string {
to := make([]string, 0, 10)
for _, field := range []AddressHeader{AddrTo, AddrCc, AddrBcc} {
if a[field] != nil {
for _, address := range a[field] {
to = append(to, address.Address)
}
}
}
return to
}
// EffectiveSender returns the first "sender" entry, if there is none it returns
// the "first" from entry, if that is empty it returns a NoSender error.
func (a Addresses) EffectiveSender() (string, error) {
if sender, ok := a[AddrSender]; ok {
if len(sender) > 0 {
return sender[0].Address, nil
}
}
if from, ok := a[AddrFrom]; ok {
if len(from) > 0 {
return from[0].Address, nil
}
}
return "", new(NoSender)
}
// Sender adds the given name, address pair to Sender.
func (a *Addresses) Sender(name, address string) error {
return a.AddPerson(AddrSender, name, address)
}
// SenderAddr adds the given address to Sender.
func (a *Addresses) SenderAddr(address mail.Address) error {
return a.AddAddress(AddrSender, address)
}
// From adds the given name, address pair to From.
func (a *Addresses) From(name, address string) error {
return a.AddPerson(AddrFrom, name, address)
}
// FromAddr adds the given address to From.
func (a *Addresses) FromAddr(address mail.Address) error {
return a.AddAddress(AddrFrom, address)
}
// To adds the given name, address pair to To.
func (a *Addresses) To(name, address string) error {
return a.AddPerson(AddrTo, name, address)
}
// ToAddr adds the given address to To.
func (a *Addresses) ToAddr(address mail.Address) error {
return a.AddAddress(AddrTo, address)
}
// Cc adds the given name, address pair to Cc.
func (a *Addresses) Cc(name, address string) error {
return a.AddPerson(AddrCc, name, address)
}
// CcAddr adds the given address to Cc.
func (a *Addresses) CcAddr(address mail.Address) error {
return a.AddAddress(AddrCc, address)
}
// Bcc adds the given name, address pair to Bcc.
func (a *Addresses) Bcc(name, address string) error {
return a.AddPerson(AddrBcc, name, address)
}
// BccAddr adds the given address to Bcc.
func (a *Addresses) BccAddr(address mail.Address) error {
return a.AddAddress(AddrBcc, address)
}
// ReplyTo adds the given name, address pair to ReplyTo.
func (a *Addresses) ReplyTo(name, address string) error {
return a.AddPerson(AddrReplyTo, name, address)
}
// ReplyToAddr adds the given address to ReplyTo.
func (a *Addresses) ReplyToAddr(address mail.Address) error {
return a.AddAddress(AddrReplyTo, address)
}
// FollowupTo adds the given name, address pair to FollowupTo.
func (a *Addresses) FollowupTo(name, address string) error {
return a.AddPerson(AddrFollowupTo, name, address)
}
// FollowupToAddr adds the given address to FollowupTo.
func (a *Addresses) FollowupToAddr(address mail.Address) error {
return a.AddAddress(AddrFollowupTo, address)
}
// AddPerson adds the given details to the given mail header field.
// Field should be a valid address field. Use the predefined Addr... constants or
// the corresponding methods.
// Adding the address will fail if the field is not one of the predefined constants.
func (a *Addresses) AddPerson(field AddressHeader, name, address string) error {
return a.AddAddress(field, mail.Address{Name: name, Address: address})
}
// AddAddress adds a recipient to your mail Header.
// Field should be a valid address field. Use the predefined Addr... constants or
// the corresponding methods.
// Adding the address will fail if field is not one of the predefined constants.
func (a *Addresses) AddAddress(field AddressHeader, address mail.Address) error {
if !valid(field) {
return InvalidField(field)
}
(*a)[field] = append((*a)[field], address)
return nil
}
func valid(field AddressHeader) bool {
switch field {
case AddrSender, AddrFrom, AddrTo, AddrCc, AddrBcc, AddrReplyTo, AddrFollowupTo:
return true
default:
return false
}
}
// ToMimeHeader packs the contents up in the given MIMEHeader or creates a new
// one if nil is passed.
func (a Addresses) ToMimeHeader(part textproto.MIMEHeader) textproto.MIMEHeader {
if part == nil {
part = make(textproto.MIMEHeader)
}
for field, addresses := range a {
if addresses != nil {
for _, address := range addresses {
part.Add(string(field), address.String())
}
}
}
return part
}