dstew32424 2018-04-03 08:14
浏览 85

如何以Sendgrid API接受的格式将html嵌入json中?

I have been trying to send an HTML email using the Sendgrid API, but I have been unsuccessful in embedding the html inside the json request.

This is an example of the html I am trying to send (emailtpl):

<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body></body></html>

Things I have tried:

  1. html.EscapeString(emailtpl)
  2. strconv.Quote(emailtpl)
  3. using backticks ` inside the json template
  4. wrapping value with single quotes for the value in the json template.
  5. base64.StdEncoding.EncodeToString([]byte(emailtpl)) only display the base64 gibberish.

Items #1 and #5 are the only solutions that Sendgrid has accepted, but the html sent is not correct (as shown in the screenshot).

escape-butchered

Items #2 - #4 all result in Status 400 Bad Request.

Does anyone know how to embed html into Sendgrid API request that Sendgrid accepts AND it renders correctly?

  • 写回答

1条回答 默认 最新

  • doushi1960 2018-04-06 15:59
    关注

    Looking at the Sendgrid api docs, it looks like it should accept html. You will need to have the html properly escaped in the json string (and set the content->type = "text/html").

    In your example template, the only problem I see are the double quotes in your meta tag. As a quick test to make sure everything is working, I would attempt to send the following html string:

    <html><head></head><body>Hi!</body></html>
    

    If that html string is successful, then you need to work on escaping your original html string example. The only invalid characters I see are the double quotes, which need to be escaped in json with backslashes. I'm not sure if go has a specific function for this, but it looks like this should work:

    // import "strings"
    
    strings.Replace(emailtpl, `"`, `\"`, -1)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用