【发布时间】:2018-07-18 12:04:13
【问题描述】:
我正在创建一个脚本,该脚本可通过 Google 表格中的一个按钮访问,单击该按钮会发送电子邮件。我想使用 SendGrid 来发送我的邮件,而不是 Google 的 MailApp 或 Gmail。关于如何将 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