google app engine - Go sendgrid failing -


im trying send mail sendgrid , golang. im using "github.com/sendgrid/sendgrid-go"

whenever run part sending email in code, error:

sendgrid.go: error:net/http: client transport of type init.failingtransport doesn't support cancelrequest; timeout not supported; response:<nil> 

i cant figure out is..

this code:

sg := sendgrid.newsendgridclientwithapikey("string")     email := sendgrid.newmail()     email.addto(req.email)     email.setsubject("confirm registration @ domain")     email.setfrom("noreply@domain.com")     email.setfromname("domain")     email.settext(url)     if r := sg.send(email); r == nil {         log.infof(ctx, "email sent")     } else {         log.errorf(ctx, "email not sent %v", r)         w.writeheader(http.statusinternalservererror)         return     } 

normal sendgrid api usage in go

the minimal program use sendgrid api in go following:

package main  import (     "fmt"     "github.com/sendgrid/sendgrid-go" )  func main() {     key := "sendgrid_api_key"     sg := sendgrid.newsendgridclientwithapikey(key)      message := sendgrid.newmail()     message.addto("ezra@usefultiftrue.com")     message.setfrom("test@example.org")     message.setsubject("subject here")     message.sethtml("body of email")      if r := sg.send(message); r == nil {         fmt.println("sent!")     } else {         fmt.println(r)     } } 

you can see sendgrid go documentation on github.

quirks on app engine

the issue you're attempting fetch url using default net/http client, not allowed app engine. you must use urlfetch make http requests on app engine.

you can solve specifying *http.client use urlfetch, since sendgrid uses client uses disallowed http.defaulttransport default. init.failingtransport dummy transport method throws error.

this simple change. import "appengine/urlfetch" , add

client := urlfetch.client(ctx) 

a complete example of sendgrid go api on app engine

package ezralalonde  import (     "appengine"     "appengine/urlfetch"     "fmt"     "github.com/sendgrid/sendgrid-go"     "net/http" )  func send(w http.responsewriter, r *http.request) {     key := "sendgrid_api_key"     sg := sendgrid.newsendgridclientwithapikey(key)      // must change net/http client not use default transport     ctx := appengine.newcontext(r)     client := urlfetch.client(ctx)     sg.client = client // <-- using urlfetch, "overriding" default      message := sendgrid.newmail()     message.addto("ezra@usefultiftrue.com")     message.setfrom("test@example.org")     message.setsubject("subject here")     message.sethtml("body of email")      if e := sg.send(message); e == nil {         fmt.fprintln(w, "sent!")     } else {         fmt.fprintln(w, e)     } }  func init() {     http.handlefunc("/", send) } 

now each visit / cause email sent sendgrid test@example.org.

to: test@example.org from: ezra@usefuliftrue.com subject: subject here  body of email 

please note

do not include api keys in source code, or commit them source control. above method not advisable.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -