【问题标题】:Content not added to SendGrid template email before it is sent发送前未将内容添加到 SendGrid 模板电子邮件
【发布时间】:2018-07-18 12:04:13
【问题描述】:

我正在创建一个脚本,该脚本可通过 Google 表格中的一个按钮访问,单击该按钮会发送电子邮件。我想使用 SendGrid 来发送我的邮件,而不是 Google 的 MailAppGmail。关于如何将 SendGrid 与 Google Apps 脚本一起使用的文档并不多。

我需要使用 SendGrid 模板,但是当我将它作为参数传递时,发送的消息仅包含模板的标头而不包含内容。为什么会发生这种情况,我该如何解决?


var SENDGRID_KEY ='My_key';
var headers = {
  "Authorization" : "Bearer "+ SENDGRID_KEY,
  "Content-Type": "application/json" 
};

var body = {
  "personalizations": [
    { "to": [
        { "email": "name@domain"
        }
      ],
      "subject": "Hello, World!",
    }
  ],
  "from": {
     "email": "name@domain"
  },
  "content": [
    { "type": "text",
      "value": "Hello, World!"
    }
  ],
  "template_id": "My_template_id"
};

var options = {
  'method': 'post',
  'headers': headers,
  'payload': JSON.stringify(body)
}
var response = UrlFetchApp.fetch("https://api.sendgrid.com/v3/mail/send", options);
Logger.log(response);

【问题讨论】:

    标签: google-apps-script sendgrid


    【解决方案1】:

    Sendgrid API 需要 MIME 类型的“内容”属性的“类型”,例如“text/plain”或“text/html”。

    尝试将"type": "text" 更改为"type":"text/plain"

    【讨论】:

    • 非常感谢卡梅伦!通过将“text”替换为“text/html”,它可以完美地工作!干杯!
    • 很高兴听到它成功了!单击答案左侧的复选标记将其标记为“已接受”并确认您找到了解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多