sendgrid-go: Send mail failed with v3 API and multi-byte contents.

Issue Summary

We can’t send a mail via v3 API with multibyte content.

Steps to Reproduce

func TestSendGrid_bare(t *testing.T) {
    m := mail.NewV3Mail()
    m.SetFrom(mail.NewEmail("日本語名1", "foobar@example.com"))
    m.Subject = "日本語タイトル"
    m.AddContent(mail.NewContent("text/html", "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"))

    p := mail.NewPersonalization()
    p.AddTos(mail.NewEmail("日本語名2", "fizzbuzz@example.com.com"))
    m.AddPersonalizations(p)

    request := sendgrid.GetRequest("<my api key>", "/v3/mail/send", "https://api.sendgrid.com")
    request.Method = "POST"
    request.Body = mail.GetRequestBody(m)

    response, err := sendgrid.API(request)
    if err != nil {
        t.Error(err)
    } else if response.StatusCode < 200 || 300 <= response.StatusCode{
        t.Error(response.StatusCode)
        t.Error(response.Body)
        t.Error(response.Headers)
    } else {
        t.Log(response.StatusCode)
        t.Log(response.Body)
        t.Log(response.Headers)
    }
}

func TestSendGrid_emoji(t *testing.T) {
    m := mail.NewV3Mail()
    m.SetFrom(mail.NewEmail("foo", "foobar@example.com"))
    m.Subject = "title"
    m.AddContent(mail.NewContent("text/plain", "🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸🐱😸"))

    p := mail.NewPersonalization()
    p.AddTos(mail.NewEmail("fizzbuzz", "fizzbuzz@example.com"))
    m.AddPersonalizations(p)

    request := sendgrid.GetRequest("<my api key>", "/v3/mail/send", "https://api.sendgrid.com")
    request.Method = "POST"
    request.Body = mail.GetRequestBody(m)

    response, err := sendgrid.API(request)
    if err != nil {
        t.Error(err)
    } else if response.StatusCode < 200 || 300 <= response.StatusCode{
        t.Error(response.StatusCode)
        t.Error(response.Body)
        t.Error(response.Headers)
    } else {
        t.Log(response.StatusCode)
        t.Log(response.Body)
        t.Log(response.Headers)
    }
}
=== RUN   TestSendGrid_bare
--- FAIL: TestSendGrid_bare (0.89s)
    sendgrid_test.go:30: 415
    sendgrid_test.go:31: {"errors":[{"message":"Unrecognized UTF8 Byte at position 511","field":null,"help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#-Encoding-Errors"}]}
    sendgrid_test.go:32: map[Server:[nginx] Date:[Thu, 23 Jun 2016 04:21:42 GMT] Content-Type:[application/json] Content-Length:[171] Connection:[keep-alive] X-Frame-Options:[DENY]]
=== RUN   TestSendGrid_emoji
--- FAIL: TestSendGrid_emoji (0.09s)
    sendgrid_test.go:59: 415
    sendgrid_test.go:60: {"errors":[{"message":"Unrecognized UTF8 Byte at position 511","field":null,"help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#-Encoding-Errors"}]}
    sendgrid_test.go:61: map[Content-Length:[171] Connection:[keep-alive] X-Frame-Options:[DENY] Server:[nginx] Date:[Thu, 23 Jun 2016 04:21:42 GMT] Content-Type:[application/json]]

Technical details:

  • sendgrid-go Version: master (latest commit: [21f08698ba61551befffd8755f508631a4d1e8ae])
  • Go Version: 1.6.2

I’m posted this issue to SendGrid support. Request # 691762

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 6
  • Comments: 15 (8 by maintainers)

Most upvoted comments

@thinkingserious I think this issue is not client side bug. It can reproduce by cURL.

$ curl --request POST --url https://api.sendgrid.com/v3/mail/send --header 'Authorization: Bearer <my api key>' --header 'Content-Type: application/json' --data '{"from":{"name":"unit test","email":"foobar@example.com"},"subject":"unit test","personalizations":[{"to":[{"email":"fizzbuzz@example.com"}]}],"content":[{"type":"text/plain","value":"テキストだよ"},{"type":"text/html","value":"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234 567890123456789012345678901234567890123456789012345678901234567890123456789 012345678901234567890123456789012345678901234567890123456789012345678901234 567890123456789012345678901234567890123456789012345678901234567890123456789 012345678901234567890123456789012345678901234567890123456789012345678901234 5678901234567890123456789012345678901234567890123456789"}]}'
{"errors":[{"message":"Unrecognized UTF8 Byte at position 1023","field":null,"help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#-Encoding-Errors"}]}

@norbergj,

I don’t have an ETA, but the continued comments and votes help move it up the queue. Also, I will be advocating again for a quick fix today at our stand up.

@Rompei,

Thanks! I’ll pass this along to the team.

@thinkingserious I got the same problem when I try to send email written in Japanese then email was not delivered and the error was appeared.

I think it is caused by the length of body. Please see the examples below.

package main

import (
    "fmt"
    "github.com/sendgrid/sendgrid-go"
    "github.com/sendgrid/sendgrid-go/helpers/mail"
    "os"
)

func main() {
    from := mail.NewEmail("sender", "info@example.com")
    to := mail.NewEmail("receiver", "example@example.com")
    content := mail.NewContent("text/plain", "こんにちは")
    m := mail.NewV3MailInit(from, "テスト", to, content)

    request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
    request.Method = "POST"
    request.Body = mail.GetRequestBody(m)
    resp, err := sendgrid.API(request)
    if err != nil {
        panic(err)
    }
    fmt.Println(resp.StatusCode)
    fmt.Println(resp.Body)
    fmt.Println(resp.Headers)
}

It’s ok. but

package main

import (
    "fmt"
    "github.com/sendgrid/sendgrid-go"
    "github.com/sendgrid/sendgrid-go/helpers/mail"
    "os"
)

func main() {
    from := mail.NewEmail("sender", "info@example.com")
    to := mail.NewEmail("receiver", "example@example.com")
    content := mail.NewContent("text/plain", "こんにちはああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああ!!!!")
    m := mail.NewV3MailInit(from, "テスト", to, content)

    request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
    request.Method = "POST"
    request.Body = mail.GetRequestBody(m)
    resp, err := sendgrid.API(request)
    if err != nil {
        panic(err)
    }
    fmt.Println(resp.StatusCode)
    fmt.Println(resp.Body)
    fmt.Println(resp.Headers)
}

It outputs the error

415
{"errors":[{"message":"Unrecognized UTF8 Byte at position 1023","field":null,"help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#-Encoding-Errors"}]}
map[Server:[nginx] Date:[Thu, 14 Jul 2016 11:06:56 GMT] Content-Type:[application/json] Content-Length:[172] Connection:[keep-alive]]

And, it’s appeared in a byte of 2 of the multiplier - 1, for example 511 and 1023.

I hope it will be your help, and I use v2.0.0 with glide until it be fixed.