【问题标题】:Send HTML content via SendGrid v3通过 SendGrid v3 发送 HTML 内容
【发布时间】:2017-02-16 09:59:41
【问题描述】:

我使用 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>"
    }
  ], 

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

【问题讨论】:

  • 问题可能是你的value里面的",你必须把&amp;quot;放在里面

标签: go sendgrid email sendgrid-api-v3


【解决方案1】:

需要这样做:

<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>"
        }
      ], 

【讨论】:

  • 是的,您需要转义您的 in-html 引号。
【解决方案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)
}

sendgrid-go

helpers sample

【讨论】:

    猜你喜欢
    • 2016-08-09
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    相关资源
    最近更新 更多