@@ -7,9 +7,12 @@ import (
77 "net"
88 "net/http"
99 "net/url"
10+ "strings"
1011 "time"
1112)
1213
14+ const Version = "0.5.1"
15+
1316func timeoutHandler (network , address string ) (net.Conn , error ) {
1417 return net .DialTimeout (network , address , time .Duration (5 * time .Second ))
1518}
@@ -87,18 +90,24 @@ func (sg *SGClient) Send(m *SGMail) error {
8790 if e != nil {
8891 return e
8992 }
90- r , e := sg .Client .PostForm (sg .APIMail , values )
93+ req , e := http .NewRequest ("POST" , sg .APIMail , strings .NewReader (values .Encode ()))
94+ if e != nil {
95+ return e
96+ }
97+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
98+ req .Header .Set ("User-Agent" , "sendgrid-go/" + Version + ";go" )
99+ res , e := sg .Client .Do (req )
91100 if e != nil {
92- return fmt .Errorf ("sendgrid.go: error:%v; response:%v" , e , r )
101+ return fmt .Errorf ("sendgrid.go: error:%v; response:%v" , e , res )
93102 }
94-
95- defer r .Body .Close ()
96-
97- if r .StatusCode == http .StatusOK {
103+
104+ defer res .Body .Close ()
105+
106+ if res .StatusCode == http .StatusOK {
98107 return nil
99108 }
100-
101- body , _ := ioutil .ReadAll (r .Body )
102-
103- return fmt .Errorf ("sendgrid.go: code:%d error:%v body:%s" , r .StatusCode , e , body )
109+
110+ body , _ := ioutil .ReadAll (res .Body )
111+
112+ return fmt .Errorf ("sendgrid.go: code:%d error:%v body:%s" , res .StatusCode , e , body )
104113}
0 commit comments