Я обновил свою версию golang с 1.9 до 1.11. После обновления sendgrid отправка почты не работает.
Я перешел по ссылке ниже:
https://cloud.google.com/appengine/docs/standard/go111/go-differences
и обнаружил, что нам нужно использовать request.Context () или предпочитаемый вами контекст вместо использования appengine.NewContext.
Но когда я пытаюсь сделать запрос request.Context () не определяется.
Так как же изменить appengine.NewContext на request.Context () для go111
Вот мой код:
func SendTestmail(c echo.Context) error {
type output struct {
Message string `json:"message"`
Status bool `json:"status"`
}
result := output{}
//mail code
to := "myemail@mail.com"
firstName := c.FormValue("first_name")
subject := "Send Test mail"
msg := "Dear User , " + "\n \n" +
"You have successfully Tested." + "\n " +
"Sincerely, " + "\n \n" +
"Team"
var Body = general.FrameEmailObj(os.Getenv("SENDGRID_SENDER"), subject, to, msg)
url := os.Getenv("SENDGRID_URL")
req, err := http.NewRequest("POST", url, bytes.NewBuffer(Body))
req.Header.Set("Authorization", "Bearer "+os.Getenv("SENDGRID_API_KEY"))
req.Header.Set("Content-Type", "application/json")
ctx := appengine.NewContext(c.Request())
client := urlfetch.Client(ctx)
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
//end mail code
result.Message = "send mail success."
return c.JSON(http.StatusUnauthorized, result)
}
Я получаю ниже ошибку в appengine: -
PANIC RECOVER Post https://api.sendgrid.com/v3/mail/send: не относится к контексту goroutine App Engine
Заранее спасибо за помощь.