gpt4 book ai didi

go - sendgrid go send get 错误意外的 EOF

转载 作者:行者123 更新时间:2023-12-03 10:06:39 25 4
gpt4 key购买 nike

我正在使用 go 发送邮件杂志,我尝试使用 sendgrid我在 https://github.com/sendgrid/sendgrid-go 中尝试了示例在 main() 上运行一切正常

示例代码如下


package main

import (
"fmt"
"log"
"os"

"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
)

func main() {
from := mail.NewEmail("Example User", "test@example.com")
subject := "Sending with Twilio SendGrid is Fun"
to := mail.NewEmail("Example User", "test@example.com")
plainTextContent := "and easy to do anywhere, even with Go"
htmlContent := "<strong>and easy to do anywhere, even with Go</strong>"
message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
client := sendgrid.NewSendClient(os.Getenv("SENDGRID_API_KEY"))
response, err := client.Send(message)
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}

但我需要发送 HTML 文本到邮件杂志

所以我需要修改

htmlContent := "<strong>and easy to do anywhere, even with Go</strong>"


htmlContent := "<div>some thing I want to send</div>"

所以我做了以下

func content() string {
return fmt.Sprint(`<div>many many html here</div>`)
}


func main() {
key := "my key of sendgrid"
send(key)
}


func send(key string){
from := mail.NewEmail("Example User", "test@example.com")
subject := "Sending with Twilio SendGrid is Fun"
to := mail.NewEmail("Example User", "xu1718191411@yahoo.co.jp")
plainTextContent := "and easy to do anywhere, even with Go"
htmlContent := content()
message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
client := sendgrid.NewSendClient(key)
response, err := client.Send(message)
if err != nil {
fmt.Println("==========send1 failed with",err)
}

fmt.Println("send1 result")
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}

func main() {
key := "my key here"
send(key)
}


func send(key string){
from := mail.NewEmail("Example User", "test@example.com")
subject := "Sending with Twilio SendGrid is Fun"
to := mail.NewEmail("Example User", "my email")
plainTextContent := "and easy to do anywhere, even with Go"
htmlContent := content()
message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
client := sendgrid.NewSendClient(key)
response, err := client.Send(message)
if err != nil {
fmt.Println("==========send1 failed with",err)
}

fmt.Println("send1 result")
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}

我得到了以下错误

==========send1 failed with unexpected EOF
send result
401

map[Access-Control-Allow-Methods:[POST] Access-Control-Allow-Headers:[Authorization, Content-Type, On-behalf-of, x-sg-elas-acl] Date:[Thu, 03 Oct 2019 08:01:36 GMT] Content-Length:[76] Connection:[keep-alive] Access-Control-Max-Age:[600] X-No-Cors-Reason:[https://sendgrid.com/docs/Classroom/Basics/API/cors.html] Server:[nginx] Content-Type:[application/json] Access-Control-Allow-Origin:[https://sendgrid.api-docs.io]]
send1_with_mini_data result

看来如果HTML字符串的长度超过一定长度,就会出现意想不到的EOF。

如果我只是发送几个Html字符串,这个过程会很顺利

喜欢


func content() string {
return fmt.Sprint(`<div>one word</div> `)
}

我计算过它取决于 HTML 字符串的长度。如果长度大于2401,会出现EOF错误


func content() string {
result := fmt.Sprint(`<div>some html string</div> `)
log.Print("length of the html string length")
log.Print(len(result))
}

我明白了

2019/10/03 17:31:05 length of the html string length
2019/10/03 17:31:05 2401
send1 result
401
{"errors":[{"message":"Maximum credits exceeded","field":null,"help":null}]}
map[Date:[Thu, 03 Oct 2019 08:31:05 GMT] Content-Type:[application/json] Content-Length:[76] Connection:[keep-alive] Access-Control-Allow-Origin:[https://sendgrid.api-docs.io] X-No-Cors-Reason:[https://sendgrid.com/docs/Classroom/Basics/API/cors.html] Server:[nginx] Access-Control-Allow-Methods:[POST] Access-Control-Allow-Headers:[Authorization, Content-Type, On-behalf-of, x-sg-elas-acl] Access-Control-Max-Age:[600]]
send1_with_mini_data result

还有这个

2019/10/03 17:31:47 length of the html string length
2019/10/03 17:31:47 2402
==========send1 failed with unexpected EOF
send1 result
401

map[Content-Type:[application/json] Content-Length:[76] Connection:[keep-alive] Access-Control-Allow-Origin:[https://sendgrid.api-docs.io] Access-Control-Max-Age:[600] Server:[nginx] Date:[Thu, 03 Oct 2019 08:31:47 GMT] Access-Control-Allow-Methods:[POST] Access-Control-Allow-Headers:[Authorization, Content-Type, On-behalf-of, x-sg-elas-acl] X-No-Cors-Reason:[https://sendgrid.com/docs/Classroom/Basics/API/cors.html]]
send1_with_mini_data result

我的环境去 1.11.6

发送网格 3.1.0

谁能帮帮我?

最佳答案

我使用的是测试帐户。而且它只允许我每天发送 100 封邮件。我开始付款,它就消失了。

怪sendgrid react 差。

关于go - sendgrid go send get 错误意外的 EOF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58214961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com