File tree Expand file tree Collapse file tree 3 files changed +78
-1
lines changed
Expand file tree Collapse file tree 3 files changed +78
-1
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import (
1313type 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
82115func (m * SGMail ) SetSubject (subject string ) {
83116 m .Subject = subject
Original file line number Diff line number Diff 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+
89130func TestSetSubject (t * testing.T ) {
90131 m := NewMail ()
91132 testSubject := "Subject"
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ import (
1111 "time"
1212)
1313
14- const Version = "0.5.1 "
14+ const Version = "1.0.0 "
1515
1616func 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 }
You can’t perform that action at this time.
0 commit comments