问题
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