4

我使用 GO 并尝试通过 sendgrid API v3 发送邮件(https://github.com/sendgrid/sendgrid-go没有邮件帮助程序类)。但是当我使用这段代码时:

"content": [
    {
      "type": "text/html", 
      "value": "<html><head></head><body>Hello You link <a href="http://example.com/reschedule?id=12334">Click</a></body></html>"
    }
  ], 

我得到错误:

400 {"errors":[{"message":"Bad Request","field":null,"help":null}]}

但是这段代码可以正常工作:

"content": [
    {
      "type": "text/html", 
      "value": "<html><head></head><body>Hello!</body></html>"
    }
  ], 

我认为特殊字符有问题,但我该如何解决?谢谢!

4

2 回答 2

4

需要这样做:

<div class=\"ad_box\"><img class=\"banner\" src=\"some_ad.png\" alt=\"\" \/>

Example
    "content": [
        {
          "type": "text/html", 
          "value": "<html><head></head><body>Hello You link <a href=\"http://example.com/reschedule?id=12334\">Click</a></body></html>"
        }
      ], 
于 2017-02-16T11:32:15.290 回答
2

我遇到了同样的问题,同样的转义特殊字符,解决的是使用官方客户端去及其助手。

示例代码:

from := mail.NewEmail("from", "from@mail.com")
to := mail.NewEmail("to", "to@mail.com")
content := mail.NewContent("text/html", contentHtml)

email := mail.NewV3MailInit(from, "Sample about sendgrid client", to, content)

personalization := mail.NewPersonalization()
personalization.AddTos(to)
email.AddPersonalizations(personalization)

client := sendgrid.NewSendClient(os.Getenv("SENDGRID_API_KEY"))
response, err = client.Send(email)
if err != nil {
    return fmt.Errorf("Cannot send the email: %v", err)
}

if response.StatusCode != 202 {
    return fmt.Errorf("Cannot send the email: %s", response.Body)
}

发送网格去

帮手样本

于 2019-10-30T20:33:58.420 回答