Я использую 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
sendgrid 3.1.0
кто-нибудь может мне помочь?