Skip to content

Commit b39babe

Browse files
author
elbuo8
committed
Added CC support with tests
1 parent 45937ab commit b39babe

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

mail.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
type SGMail struct {
1414
To []string
1515
ToName []string
16+
Cc []string
1617
Subject string
1718
Text string
1819
HTML string
@@ -78,6 +79,38 @@ func (m *SGMail) AddToNames(names []string) {
7879
m.ToName = append(m.ToName, names...)
7980
}
8081

82+
// AddCc ...
83+
func (m *SGMail) AddCc(cc string) error {
84+
address, err := mail.ParseAddress(cc)
85+
if err != nil {
86+
return err
87+
}
88+
m.AddCcRecipient(address)
89+
return nil
90+
}
91+
92+
// AddCcs ...
93+
func (m *SGMail) AddCcs(ccs []string) error {
94+
for i := 0; i < len(ccs); i++ {
95+
if err := m.AddCc(ccs[i]); err != nil {
96+
return err
97+
}
98+
}
99+
return nil
100+
}
101+
102+
// AddCcRecipient ...
103+
func (m *SGMail) AddCcRecipient(recipient *mail.Address) {
104+
m.Cc = append(m.Cc, recipient.Address)
105+
}
106+
107+
// AddCcRecipients ...
108+
func (m *SGMail) AddCcRecipients(recipients []*mail.Address) {
109+
for i := 0; i < len(recipients); i++ {
110+
m.AddCcRecipient(recipients[i])
111+
}
112+
}
113+
81114
// SetSubject sets the email's subject
82115
func (m *SGMail) SetSubject(subject string) {
83116
m.Subject = subject

mail_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,47 @@ func TestAddToNames(t *testing.T) {
8686
}
8787
}
8888

89+
func TestAddCc(t *testing.T) {
90+
m := NewMail()
91+
m.AddCc("Email Name<email@email.com>")
92+
if len(m.Cc) != 1 {
93+
t.Errorf("AddCc should append to SGMail.Cc")
94+
}
95+
}
96+
97+
func TestAddCcFail(t *testing.T) {
98+
m := NewMail()
99+
err := m.AddCc(".com")
100+
if err == nil {
101+
t.Errorf("AddCc should fail on invalid email addresses")
102+
}
103+
}
104+
105+
func TestAddCcs(t *testing.T) {
106+
m := NewMail()
107+
m.AddCcs([]string{"Email Name <email+1@email.com>", "email+2@email.com"})
108+
if len(m.Cc) != 2 {
109+
t.Errorf("AddCcs should append to SGMail.Cc")
110+
}
111+
}
112+
113+
func TestAddCcsFail(t *testing.T) {
114+
m := NewMail()
115+
err := m.AddCcs([]string{".co", "email+2@email.com"})
116+
if err == nil {
117+
t.Errorf("AddCcs should fail in invalid email address")
118+
}
119+
}
120+
121+
func TestAddCcRecipients(t *testing.T) {
122+
m := NewMail()
123+
emails, _ := mail.ParseAddressList("Joe <email+1@email.com>, Doe <email+2@email.com>")
124+
m.AddCcRecipients(emails)
125+
if len(m.Cc) != 2 {
126+
t.Errorf("AddCcRecipients should append to SGMail.Cc")
127+
}
128+
}
129+
89130
func TestSetSubject(t *testing.T) {
90131
m := NewMail()
91132
testSubject := "Subject"

sendgrid.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"time"
1212
)
1313

14-
const Version = "0.5.1"
14+
const Version = "1.0.0"
1515

1616
func timeoutHandler(network, address string) (net.Conn, error) {
1717
return net.DialTimeout(network, address, time.Duration(5*time.Second))
@@ -60,6 +60,9 @@ func (sg *SGClient) buildURL(m *SGMail) (url.Values, error) {
6060
for i := 0; i < len(m.To); i++ {
6161
values.Add("to[]", m.To[i])
6262
}
63+
for i := 0; i < len(m.Cc); i++ {
64+
values.Add("cc[]", m.Cc[i])
65+
}
6366
for i := 0; i < len(m.Bcc); i++ {
6467
values.Add("bcc[]", m.Bcc[i])
6568
}

0 commit comments

Comments
 (0)