Reputation: 179
I am using go to send mail magazine, And I tried to use sendgrid I tried samples in https://github.com/sendgrid/sendgrid-go Everything is fine running on main()
the sample code is like following
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", "[email protected]")
subject := "Sending with Twilio SendGrid is Fun"
to := mail.NewEmail("Example User", "[email protected]")
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)
}
}
But I need to send HTML text to mail magazine
So I need to modify
htmlContent := "<strong>and easy to do anywhere, even with Go</strong>"
to
htmlContent := "<div>some thing I want to send</div>"
so I did the following
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", "[email protected]")
subject := "Sending with Twilio SendGrid is Fun"
to := mail.NewEmail("Example User", "[email protected]")
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", "[email protected]")
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)
}
And I got the following error
==========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
It seems that if the length of the HTML string exceeds a certain length, unexpected EOF will occur.
If I just send a few Html strings, the process will go smoothly
like
func content() string {
return fmt.Sprint(`<div>one word</div> `)
}
And I counted that it depends on HTML string length. If the length is greater than 2401, EOF errors will occur
func content() string {
result := fmt.Sprint(`<div>some html string</div> `)
log.Print("length of the html string length")
log.Print(len(result))
}
And I got this
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
And this
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
My environment go 1.11.6
sendgrid 3.1.0
can anyone help me?
Upvotes: 3
Views: 1491
Reputation: 353
I used a test account. And it only allows me to send 100 mails per day. I started paying and it went away.
blame sendgrid poor response.
Upvotes: 1