本文介绍了如何从Google App Engine Golang使用SendGrid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例代码位于:

我猜这是使用Google App上的sendgrid-go的非常旧的示例代码发动机。



我尝试了4个排列组合,每次都失败:

  https://api.sendgrid.com/v3/mail/send:http.DefaultTransport和http.DefaultClient在App Engine中不可用。请参阅https://cloud.google.com/appengine/docs/go/urlfetch/ 

这里是一些日志记录的最低硬编码尝试:

 包sendgridgo 


导入(
github.com/sendgrid/sendgrid-go
fmt
_google.golang.org/appengine
净/ http
google。 golang.org/appengine/log
google.golang.org/appengine
google.golang.org/appengine/urlfetch
_github.com/sendgrid/sendgrid- go / helpers / mail


func init(){
http.HandleFunc(/,IndexHandler)
appengine.Main()
}

func IndexHandler(w http.ResponseWriter,r * http.Request){
ctx:= appengine.NewContext(r)

log.Infof ctx,IndexHandler)
sg:= sendgrid.NewSendClient(SENDGRID_API_KEY)
log.Infof(ctx,%v,sg)
bob:= urlfetch.Client(ctx )

log.Infof(ctx,UrlFetchClient%v,bob)
// resp,err:= s g.Send(m)
request:= sendgrid.GetRequest(SENDGRID_API_KEY,/ v3 / mail / send,https://api.sendgrid.com)
request.Method = POST

request.Body = [] byte(`{
personalizations:[
{
to:[
{
email:[email protected]
}
],
subject:用SendGrid发送很有趣
}
],
from:{
email:[email protected]
},
content:[
{
type:text / plain,
value:在任何地方都很容易做到,即使使用Go
}
]
}`)
resp,err:= sendgrid.API(请求)

如果err!= nil {
log.Errorf(ctx,Failed%v,err)
}

fmt.Fprint(w,resp)


}


解决方案

8不同的尝试,包括尝试在Google Cloud文档中发布的使用Sendgrid博客的示例(来自Sendgrid博客的示例),并试图使用已弃用的Sendgrid api版本,我发现Sendgrid curl示例位于:



  curl --request POST \ 
--url https:/ /api.sendgrid.com/v3/mail/send \
--header'Authorization:Bearer YOUR_API_KEY'\
--header'Content-Type:application / json'\
--data'{personalizations:[{to:[{email:[email protected]}]}],from:{email:[email protected] },subject:Hello,World!,content:[{type:text / plain,value:Heya!}]}'


<$ p $

p> client:= urlfetch.Client(ctx)

request,err:= http.NewRequest(POST,https://api.sendgrid.com/v3 / mail / send,buf)
if err!= nil {
log.Errorf(ctx,Failed Request%v,request)
}
request.Header。 Set(Authorization,Bearer SENDGRID_API_KEY)
request.Header.Set(Content-Type,application / json)
resp,err:= client.Do(request)

一个复活节周末,稍后,它可以工作!


The example code at: https://sendgrid.com/blog/send-email-go-google-app-engine/

My guess this is very old sample code to use sendgrid-go on Google App Engine.

I've attempted 4 permutations and failed each time with:

https://api.sendgrid.com/v3/mail/send: http.DefaultTransport and http.DefaultClient are not available in App Engine. See https://cloud.google.com/appengine/docs/go/urlfetch/

Here is a minimum hardcoded attempt with some logging:

package sendgridgo


import(
    "github.com/sendgrid/sendgrid-go"
    "fmt"
    _"google.golang.org/appengine"
    "net/http"
    "google.golang.org/appengine/log"
    "google.golang.org/appengine"
    "google.golang.org/appengine/urlfetch"
     _ "github.com/sendgrid/sendgrid-go/helpers/mail"
)

func init(){
    http.HandleFunc("/", IndexHandler)
    appengine.Main()
}

func IndexHandler (w http.ResponseWriter, r *http.Request){
    ctx := appengine.NewContext(r)

    log.Infof(ctx, "IndexHandler")
    sg := sendgrid.NewSendClient("SENDGRID_API_KEY")
    log.Infof(ctx, "%v", sg)
    bob := urlfetch.Client(ctx)

    log.Infof(ctx, "UrlFetchClient %v", bob)
    //resp, err := sg.Send(m)
    request := sendgrid.GetRequest("SENDGRID_API_KEY", "/v3/mail/send", "https://api.sendgrid.com")
    request.Method = "POST"

    request.Body = []byte(` {
    "personalizations": [
        {
            "to": [
                {
                    "email": "[email protected]"
                }
            ],
            "subject": "Sending with SendGrid is Fun"
        }
    ],
    "from": {
        "email": "[email protected]"
    },
    "content": [
        {
            "type": "text/plain",
            "value": "and easy to do anywhere, even with Go"
        }
    ]
}`)
    resp, err := sendgrid.API(request)

    if err != nil{
        log.Errorf(ctx, "Failed %v", err)
    }

    fmt.Fprint(w, resp)


}
解决方案

After 8 different attempts, including trying an example published in Google Cloud docs for using Sendgrid, an example from Sendgrid blog, and trying to use deprecated versions of Sendgrid api, I found Sendgrid curl examples at:

https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/curl_examples.html

curl --request POST \
  --url https://api.sendgrid.com/v3/mail/send \
  --header 'Authorization: Bearer YOUR_API_KEY' \
  --header 'Content-Type: application/json' \
  --data '{"personalizations": [{"to": [{"email": "[email protected]"}]}],"from": {"email": "[email protected]"},"subject": "Hello, World!","content": [{"type": "text/plain", "value": "Heya!"}]}'

I then translated the HelloWorld example to into URLFetch usage

client := urlfetch.Client(ctx)

request, err := http.NewRequest("POST", "https://api.sendgrid.com/v3/mail/send", buf)
    if err != nil {
        log.Errorf(ctx, "Failed Request %v", request)
    }
    request.Header.Set("Authorization", "Bearer SENDGRID_API_KEY")
    request.Header.Set("Content-Type", "application/json")
    resp, err := client.Do(request)

One Easter weekend, later, it works!

这篇关于如何从Google App Engine Golang使用SendGrid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 18:55