首页 > 解决方案 > sendgrid 去发送获取错误意外 EOF

问题描述

我正在使用 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

谁能帮我?

标签: gosendgrid

解决方案


我用的是测试账号。它只允许我每天发送 100 封邮件。我开始付款,它消失了。

怪sendgrid反应不佳。


推荐阅读