Debian Codegraph @

setters.go

raw
1 package mailyak
2
3 import (
4 "mime"
5 )
6
7 // To sets a list of recipient addresses.
8 //
9 // You can pass one or more addresses to this method, all of which are viewable to the recipients.
10 //
11 // mail.To("[email protected]", "[email protected]")
12 //
13 // or pass a slice of strings:
14 //
15 // tos := []string{
16 // "[email protected]",
17 // "John Doe <[email protected]>"
18 // }
19 //
20 // mail.To(tos...)
21 func (m *MailYak) To(addrs ...string) {
22 m.toAddrs = []string{}
23
24 for _, addr := range addrs {
25 trimmed := m.trimRegex.ReplaceAllString(addr, "")
26 if trimmed == "" {
27 continue
28 }
29
30 m.toAddrs = append(m.toAddrs, trimmed)
31 }
32 }
33
34 // Bcc sets a list of blind carbon copy (BCC) addresses.
35 //
36 // You can pass one or more addresses to this method, none of which are viewable to the recipients.
37 //
38 // mail.Bcc("[email protected]", "[email protected]")
39 //
40 // or pass a slice of strings:
41 //
42 // bccs := []string{
43 // "[email protected]",
44 // "John Doe <[email protected]>"
45 // }
46 //
47 // mail.Bcc(bccs...)
48 func (m *MailYak) Bcc(addrs ...string) {
49 m.bccAddrs = []string{}
50
51 for _, addr := range addrs {
52 trimmed := m.trimRegex.ReplaceAllString(addr, "")
53 if trimmed == "" {
54 continue
55 }
56
57 m.bccAddrs = append(m.bccAddrs, trimmed)
58 }
59 }
60
61 // WriteBccHeader writes the BCC header to the MIME body when true. Defaults to
62 // false.
63 //
64 // This is usually required when writing the MIME body to an email API such as
65 // Amazon's SES, but can cause problems when sending emails via a SMTP server.
66 //
67 // Specifically, RFC822 says:
68 //
69 // Some systems may choose to include the text of the "Bcc" field only in the
70 // author(s)'s copy, while others may also include it in the text sent to
71 // all those indicated in the "Bcc" list.
72 //
73 // This ambiguity can result in some SMTP servers not stripping the BCC header
74 // and exposing the BCC addressees to recipients. For more information, see:
75 //
76 // https://github.com/domodwyer/mailyak/issues/14
77 func (m *MailYak) WriteBccHeader(shouldWrite bool) {
78 m.writeBccHeader = shouldWrite
79 }
80
81 // Cc sets a list of carbon copy (CC) addresses.
82 //
83 // You can pass one or more addresses to this method, which are viewable to the other recipients.
84 //
85 // mail.Cc("[email protected]", "[email protected]")
86 //
87 // or pass a slice of strings:
88 //
89 // ccs := []string{
90 // "[email protected]",
91 // "John Doe <[email protected]>"
92 // }
93 //
94 // mail.Cc(ccs...)
95 func (m *MailYak) Cc(addrs ...string) {
96 m.ccAddrs = []string{}
97
98 for _, addr := range addrs {
99 trimmed := m.trimRegex.ReplaceAllString(addr, "")
100 if trimmed == "" {
101 continue
102 }
103
104 m.ccAddrs = append(m.ccAddrs, trimmed)
105 }
106 }
107
108 // From sets the sender email address.
109 //
110 // Users should also consider setting FromName().
111 func (m *MailYak) From(addr string) {
112 m.fromAddr = m.trimRegex.ReplaceAllString(addr, "")
113 }
114
115 // FromName sets the sender name.
116 //
117 // If set, emails typically display as being from:
118 //
119 // From Name <[email protected]>
120 //
121 // If name contains non-ASCII characters, it is Q-encoded according to RFC1342.
122 func (m *MailYak) FromName(name string) {
123 m.fromName = mime.QEncoding.Encode("UTF-8", m.trimRegex.ReplaceAllString(name, ""))
124 }
125
126 // ReplyTo sets the Reply-To email address.
127 //
128 // Setting a ReplyTo address is optional.
129 func (m *MailYak) ReplyTo(addr string) {
130 m.replyTo = m.trimRegex.ReplaceAllString(addr, "")
131 }
132
133 // Subject sets the email subject line.
134 //
135 // If sub contains non-ASCII characters, it is Q-encoded according to RFC1342.
136 func (m *MailYak) Subject(sub string) {
137 m.subject = mime.QEncoding.Encode("UTF-8", m.trimRegex.ReplaceAllString(sub, ""))
138 }
139
140 // AddHeader adds an arbitrary email header.
141 // It appends to any existing values associated with key.
142 //
143 // If value contains non-ASCII characters, it is Q-encoded according to RFC1342.
144 // As always, validate any user input before adding it to a message, as this
145 // method may enable an attacker to override the standard headers and, for
146 // example, BCC themselves in a password reset email to a different user.
147 func (m *MailYak) AddHeader(name, value string) {
148 key := m.trimRegex.ReplaceAllString(name, "")
149 m.headers[key] = append(m.headers[key], mime.QEncoding.Encode("UTF-8", m.trimRegex.ReplaceAllString(value, "")))
150 }
151
152 // SetHeader sets the header entries associated with key to
153 // the single element value. It replaces any existing
154 // values associated with key.
155 //
156 // If value contains non-ASCII characters, it is Q-encoded according to RFC1342.
157 // As always, validate any user input before adding it to a message, as this
158 // method may enable an attacker to override the standard headers and, for
159 // example, BCC themselves in a password reset email to a different user.
160 func (m *MailYak) SetHeader(name, value string) {
161 m.headers[m.trimRegex.ReplaceAllString(name, "")] = []string{mime.QEncoding.Encode("UTF-8", m.trimRegex.ReplaceAllString(value, ""))}
162 }
163
164 // LocalName sets the sender domain name.
165 //
166 // If set, it is used in the EHLO/HELO command instead of the default domain
167 // (localhost, see [smtp.NewClient]). Some SMTP servers, such as the Gmail
168 // SMTP-relay, requires a proper domain name and will reject attempts to use
169 // localhost.
170 func (m *MailYak) LocalName(name string) {
171 m.localName = m.trimRegex.ReplaceAllString(name, "")
172 }