Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google app engine Go sendgrid失败_Google App Engine_Go_Sendgrid - Fatal编程技术网

Google app engine Go sendgrid失败

Google app engine Go sendgrid失败,google-app-engine,go,sendgrid,Google App Engine,Go,Sendgrid,我正试图用sendgrid和golang发送邮件。 Im使用的是“github.com/sendgrid/sendgrid go” 每当我在代码中运行发送电子邮件的部件时,都会出现以下错误: sendgrid.go: error:net/http: Client Transport of type init.failingTransport doesn't support CancelRequest; Timeout not supported; response:<nil> Go

我正试图用sendgrid和golang发送邮件。 Im使用的是
“github.com/sendgrid/sendgrid go”

每当我在代码中运行发送电子邮件的部件时,都会出现以下错误:

sendgrid.go: error:net/http: Client Transport of type init.failingTransport doesn't support CancelRequest; Timeout not supported; response:<nil>
Go中Sendgrid API的正常使用 在Go中使用Sendgrid API的最小程序如下所示:

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 THE EMAIL")

    if r := sg.Send(message); r == nil {
        fmt.Println("Sent!")
    } else {
        fmt.Println(r)
    }
}
你可以看到这张照片

应用程序引擎的怪癖 问题是,您试图使用默认的
net/http
客户端获取Url,这是App Engine不允许的

您可以通过将
*http.Client
指定为使用
urlfetch
来解决此问题,因为Sendgrid使用的客户端默认使用不允许的
http.DefaultTransport
init.failingTransport
是一种虚拟传输方法,基本上只是抛出一个错误

这最终是一个简单的改变。只需导入
“appengine/urlfetch”
并添加

client := urlfetch.Client(ctx)
AppEngine上SendGrid Go API的完整示例 请注意
不要在源代码中包含API键,也不要将它们提交给源代码管理。上述方法是不可取的。

看起来好像您得到了一个超时。如果您也尝试远程登录邮件服务器上的端口25,是否会超时?我无法使用端口25远程登录smtp.sendgrid.net。但是如果我使用另一个端口587,我可以远程登录。。也许这就是问题所在?让我重新表述一下:如果您遇到超时错误,请先验证连接,而不是代码。我不知道这个特定的服务,所以我不能确切地告诉您应该如何测试它-但是如果您使用SMTP,很可能您必须告诉您的应用程序使用587而不是25。如果您使用的是HTTP/HTTPS API,再次从验证该端口的连接开始。只是为了让人恼火:它被称为Go not golang。golang是为了浏览器友好,是的。真烦人,我还没来得及测试。该代码与sendgrid之外的另一个解决方案一起部署。每当我改变解决方案时,我都会回到这个问题上来,将来我也会这样做。非常感谢您理解我的问题并提供您的解决方案。请问您的替代供应商是谁?当然。我正在使用谷歌应用引擎自己的邮件系统。缺点是我必须创建一个单独的电子邮件用户从“noreply”发送。但它甚至比sendgrid.interest更简单。谢谢我假设你是100%的交易型的,所以这没什么大不了的,但我发现大多数专业邮件提供商的“破折号”和统计数据是我永远不想缺少的。祝你的应用程序好运!干杯
client := urlfetch.Client(ctx)
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 the net/http client to not use default transport
    ctx := appengine.NewContext(r)
    client := urlfetch.Client(ctx)
    sg.Client = client // <-- now 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 THE EMAIL")

    if e := sg.Send(message); e == nil {
        fmt.Fprintln(w, "Sent!")
    } else {
        fmt.Fprintln(w, e)
    }
}

func init() {
    http.HandleFunc("/", send)
}
To: test@example.org
From: ezra@usefuliftrue.com
Subject: SUBJECT HERE

BODY OF THE EMAIL