I'm reading through the send grid documentation about templates here and could find this:
You can send transactional templates using one of three ways:
- Using the SMTP Relay
- Including the template ID in the templates parameter of the Web API v3 Mail Send endpoint
- Using the x-smtp api parameter in the Web API v2 Mail Send endpoint
In node.js I have access to their JS SDK. However, In API the documentation it is explained only how to use the first method (SMTP relay). In the documentation of the JS SDK there is no option for sending emails using a template.
Where can I find a complete example of how to use the JS SDK of send grid to send an email using a template?
thanks.
答案 0 :(得分:1)
看起来Github存储库中存在一个问题的例子:https://github.com/sendgrid/sendgrid-nodejs/issues/252#issuecomment-232473145
var sg = require('sendgrid').SendGrid("SendGrid API Key" or <environment variable>)
function sendEmail(tempID){
var helper = require('sendgrid').mail;
from_email = new helper.Email("frome-mail@example.com")
to_email = new helper.Email("to-email@example.com")
subject = "Dummy Subject"
content = new helper.Content("text/html", "dummy content")
mail = new helper.Mail(from_email, subject, to_email, content)
substitution = new helper.Substitution("-name-", "User's Name")
mail.personalizations[0].addSubstitution(substitution)
mail.setTemplateId(tempID)
var requestBody = mail.toJSON()
var requestPost = JSON.parse(JSON.stringify(sg.emptyRequest()))
requestPost.method = 'POST'
requestPost.path = '/v3/mail/send'
requestPost.body = requestBody
sg.API(requestPost, function (response) {
console.log(response.statusCode)
console.log(response.body)
console.log(response.headers)
})
}
就我而言,它运作良好。我收到了电子邮件但响应总是空的。