我尝试通过Parse.com的SendGrid云模块与头参数发送电子邮件,以包括一些关于正在发送的电子邮件的唯一参数。
var SendGrid = require("sendgrid");
SendGrid.initialize(username, password);
SendGrid.sendEmail({
to: request.params.to,
from: request.params.from,
subject: request.params.subject,
html: request.params.html,
headers: JSON.stringify({ "unique_args": { "newsletter": request.params.newsletter }})
}, {
success: function(httpResponse) {
response.success(httpResponse);
},
error: function(httpResponse) {
response.error(httpResponse);
}
});
这会得到错误信息:JSON在headers是有效的,但不兼容
当我没有对头部进行字符串化时,我收到了这个错误:未捕获错误:Can't form encode an Object
关于SendGrid邮件API的更多信息:https://sendgrid.com/docs/API_Reference/Web_API/mail.html
关于Parse.com SendGrid Cloud Module的更多信息:https://www.parse.com/docs/cloud_modules_guide#sendgrid
谢谢! !
标题的键实际上是x-smtpapi
。对于这种情况,您也不需要对json进行字符串化。
完整的用法是:
var SendGrid = require("sendgrid");
SendGrid.initialize(username, password);
SendGrid.sendEmail({
to: request.params.to,
from: request.params.from,
subject: request.params.subject,
html: request.params.html,
"x-smtpapi": { "unique_args": { "newsletter": request.params.newsletter }}
}, {
success: function(httpResponse) {
response.success(httpResponse);
},
error: function(httpResponse) {
response.error(httpResponse);
}
});
lib文档在这里。实际上,创建一个email对象并通过提供的方法使用它更容易。