Content not added to SendGrid template email before it is sent

拜拜、爱过 提交于 2021-01-28 08:47:56

问题


I am creating a script accessed by a button in Google Sheets that when clicked, sends emails. I want to use SendGrid to send my mails rather than Google's MailApp or Gmail. There is not a lot of documentation on how to use SendGrid with Google Apps Script.

I need to use SendGrid templates, but when I pass it as a parameter, the sent message consists only of the header of the template and not the content. Why is this happening, and how can I resolve it?


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);

回答1:


The Sendgrid API requires the "type" of the "content" property by a MIME Type, such as "text/plain" or "text/html".

Try changing "type": "text" to "type":"text/plain"



来源:https://stackoverflow.com/questions/51401583/content-not-added-to-sendgrid-template-email-before-it-is-sent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!